MongoDB Stable 1.4.4Devlopment 1.5.4 hu mongo uss(huge + monstrous) 超でかい Document Database C++ Open Source GNU AGPL v3.0 Licence OSX,Linux,Windows,Solaris |32bit,64bit Development and Support:10 gen
READ SQL SELECT* FROM mycolletions Dynamic Querys db.mycollections.find();
22.
READ ORDER BY& LIMIT/OFFSET SQL SELECT * FROM db.mycollections WHERE name = “mongo” ORDER BY id DESC LIMIT 1,10; Dynamic Querys db.mycollections.find({name:”mongo”}).sort({id: -1}).skip(1).limit(10); http://www.mongodb.org/display/DOCS/Querying
Conditional Operators WHEREkey = value .find({key:value}) .find({key:{Operator:value}) WHERE key Condtion value SQL Conditions MongoDB Operators key > value key:{$gt:value} key < value key:{$lt:value} key >= value key:{$gte:value} key <= value key:{$lte:value} key <> value key:{$ne:value} key IN(value) key:{$in:[value]} key NOT IN(value) key:{$nin:[value]} key EXIST(value) key:{$exist:value} SQL Conditions MongoDB Oprators key = value key:value
25.
SELECT between SQLSELECT * FROM logs where created_at BETWEEN ‘2010-06-10’ AND ‘2010-06-12’ Dynamic Querys db.logs.find({ created_at:{$gte:new Date(2010,6,10)}, created_at:{$lte:new Date(2010,6,12)} }); もしくは db.logs.find({ $where:"this.created_at >= new Date(2010,6,10) && this.created_at <= new Date(2010,6,12)" })
26.
READ COUNT SQLSELECT COUNT(*) FROM users; Dynamic Querys db.users.count();
27.
READ DISTINCT SQLSELECT DISTINCT(name) FROM users; Dynamic Querys db.users.distinct(“name”);
28.
READ GROUP BYSQL SELECT name,sum(marks) FROM users WHERE name=“mikami” GROUP BY name; Dynamic Querys db.users.group({ key:{name:true}, cond:{name:"mikami"}, reduce: function(obj,prev){prev.msum += obj.marks}, initial: {msum:0} }); 組み込みの Map/Reduce を使う
$inc インクリメント/デクリメント or シーケンシャルナンバー SQL update entries set count= count+1 where _id = '4c2bf9d691951da31517df10’ DynamicQuerys db.entries.update( {_id:ObjectId('4c2bf9d691951da31517df10')}, {$inc:{count:1}} )
32.
$set フィールド更新 SQLupdate entries set name = ”ruby" where user_id = 1 DynamicQuerys db.entries.update({user_id:1},{$set:{name:”ruby“}}) ※ value には配列やオブジェクトも格納できるため、まるごと値が置き換わる
$push SQL ALTERTABLE entries ADD (body varchar(255)) UPDATE entries SET tag="MongoDB" where _id = 'c2bfaea91951da31517df12'’ DynamicQuerys db.entries.update( {_id:ObjectId('4c2bfaea91951da31517df12')} ,{$push:{tag:"MongoDB"}} ) tag に配列でプッシュされる。 tag = ['MongoDB'] 2回目 tag = ['MongoDB','MongoDB']
35.
$pushAll DynamicQuerys db.entries.update({_id:ObjectId('4c2bfa4c91951da31517df11')}, {$push:{tag:["Ruby","Rails","MongoDB"]}} ) まとめて格納できる。 tag = [['Ruby','Rails','MongoDB']]
36.
$addToSet DynamicQuerys db.entries.update({_id:ObjectId('4c2bfaea91951da31517df12')}, {$addToSet:{tag:”MongoDB"}} ) tag の配列にプッシュされる。値は重複しない tag = ['MongoDB'] 2回目 tag = [‘MongoDB’]
See also Documenthttp://www.mongodb.org/display/DOCS/Developer+Zone Cookbook http://cookbook.mongodb.org/index.html Video & Slides http://www.mongodb.org/display/DOCS/Events Articles http://www.mongodb.org/display/DOCS/Articles
57.
See also Slideshttp://www.slideshare.net/fuchaoqun/mongodb-in-action-2976022
MongoMapper vs Mongoid Good Support Rails2.3 is good Plugin system ActiveRecord Building code ( http://github.com/patcito/shapado ) Bad ・ Master/Slave replication Clusters does not support. See also: http://bit.ly/9diwYp
61.
MongoMapper ActiveRecord classPerson < ActiveRecord::Base end MongoMapper class Person Include MongoMapper::Document end