サンプルデータ
> db.example.remove() > for(var i =0; i<6; i++) { ... var doc = {name:'user'+i, point: i*1000}; ... db.example.save(doc); ... } >
全件検索
> db.example.find() {"_id":ObjectId("4f69766ea84ad157af92c6ab"),"name":"user0","point":0} {"_id":ObjectId("4f69766ea84ad157af92c6ac"),"name":"user1","point":1000} {"_id":ObjectId("4f69766ea84ad157af92c6ad"),"name":"user2","point":2000} {"_id":ObjectId("4f69766ea84ad157af92c6ae"),"name":"user3","point":3000} {"_id":ObjectId("4f69766ea84ad157af92c6af"),"name":"user4","point":4000} {"_id":ObjectId("4f69766ea84ad157af92c6b0"),"name":"user5","point":5000}
条件検索
> db.example.find({point:2000}) { "_id" : ObjectId("4f69766ea84ad157af92c6ad"), "name" : "user2", "point" : 2000 }
ソート(昇順)
> db.example.find().sort({point:1}) { "_id" : ObjectId("4f69766ea84ad157af92c6ab"), "name" : "user0", "point" : 0 } { "_id" : ObjectId("4f69766ea84ad157af92c6ac"), "name" : "user1", "point" : 1000 } { "_id" : ObjectId("4f69766ea84ad157af92c6ad"), "name" : "user2", "point" : 2000 } { "_id" : ObjectId("4f69766ea84ad157af92c6ae"), "name" : "user3", "point" : 3000 } { "_id" : ObjectId("4f69766ea84ad157af92c6af"), "name" : "user4", "point" : 4000 } { "_id" : ObjectId("4f69766ea84ad157af92c6b0"), "name" : "user5", "point" : 5000 }
ソート(降順)
> db.example.find().sort({point:-1}) { "_id" : ObjectId("4f69766ea84ad157af92c6b0"), "name" : "user5", "point" : 5000 } { "_id" : ObjectId("4f69766ea84ad157af92c6af"), "name" : "user4", "point" : 4000 } { "_id" : ObjectId("4f69766ea84ad157af92c6ae"), "name" : "user3", "point" : 3000 } { "_id" : ObjectId("4f69766ea84ad157af92c6ad"), "name" : "user2", "point" : 2000 } { "_id" : ObjectId("4f69766ea84ad157af92c6ac"), "name" : "user1", "point" : 1000 } { "_id" : ObjectId("4f69766ea84ad157af92c6ab"), "name" : "user0", "point" : 0 }
リミット
>db.example.find().limit(2) { "_id" : ObjectId("4f69766ea84ad157af92c6ab"), "name" : "user0", "point" : 0 } { "_id" : ObjectId("4f69766ea84ad157af92c6ac"), "name" : "user1", "point" : 1000 }
オフセット
> db.example.find().skip(4) { "_id" : ObjectId("4f69766ea84ad157af92c6af"), "name" : "user4", "point" : 4000 } { "_id" : ObjectId("4f69766ea84ad157af92c6b0"), "name" : "user5", "point" : 5000 }
リミット&オフセット(ページング)
> db.example.find().skip(2).limit(2) { "_id" : ObjectId("4f69766ea84ad157af92c6ad"), "name" : "user2", "point" : 2000 } { "_id" : ObjectId("4f69766ea84ad157af92c6ae"), "name" : "user3", "point" : 3000 }
条件演算子(条件演算子一覧はここ)
> db.example.find({point:{$gte:3000}}) { "_id" : ObjectId("4f69766ea84ad157af92c6ae"), "name" : "user3", "point" : 3000 } { "_id" : ObjectId("4f69766ea84ad157af92c6af"), "name" : "user4", "point" : 4000 } { "_id" : ObjectId("4f69766ea84ad157af92c6b0"), "name" : "user5", "point" : 5000 }
カーソル
> var cursor = db.example.find(); > cursor.hasNext() true > cursor.next() { "_id" : ObjectId("4f69766ea84ad157af92c6ab"), "name" : "user0", "point" : 0 } >while (cursor.hasNext()){printjson(cursor.next());} { "_id" : ObjectId("4f69766ea84ad157af92c6ac"), "name" : "user1", "point" : 1000 } { "_id" : ObjectId("4f69766ea84ad157af92c6ad"), "name" : "user2", "point" : 2000 } { "_id" : ObjectId("4f69766ea84ad157af92c6ae"), "name" : "user3", "point" : 3000 } { "_id" : ObjectId("4f69766ea84ad157af92c6af"), "name" : "user4", "point" : 4000 } { "_id" : ObjectId("4f69766ea84ad157af92c6b0"), "name" : "user5", "point" : 5000 }
まとめ
MongoDBのクエリー単純で簡単。
かねこ(~_~)