mongodb_クエリー操作使用_条件クエリー、where句など

8038 ワード

<?php /*   mongodb_ _ 、where ( )
  1.find()/findOne() mongodb find() findOne() , 。 ( find() ) : db.A.find()/db.A.find({})
  "{}" , A 。 :db.A.find({"a":1,"b":1}), a 1 b 1 , c 1, :db.A.find({"a":1,"b":1,"c":1})。
  2. , 10 , 。 : { "_id" : ObjectId("5018da521781352fe25bf4d2"), "a" : "1", "b" : "1", "c" : "1", "d" : "1", "e" : "1" } a,b,c : db.A.find({},{"a,":1,"b":1,"c":,"_id":0}) 1 0 , 1: ,0: 。 "_id" , 。
  { "a" : "1", "b" : "1", "c" : "1" }
     
  -------------------------------------- SQL-------------------------------------------- 3. 3.1 "$lt"===================>"<" "$lte"==================>"<=" "$gt"===================>">" "$gte"==================>">=" "$ne"===================>"!="
  : B x , 10<x<=30 , : db.B.find({"x":{"$gt":10,"$lte":30}})
  : B day 2012/01/01 , : db.B.find({"day":{"$gt":new Date("2012/01/01")}}) 、 、 。
  3.2 $in /$nin $in: ; $nin: ;
  SQL: : in (' 1',' 1'.....) mongodb:db.B.find({"x":{"$in":[' 1',' 2',.....]}})
  SQL: : not in (' 1',' 1'.....) mongodb:db.B.find({"x":{"$nin":[' 1',' 2',.....]}})
  $in/$nin : 。
  3.3 $or
  $or: ;
  SQL: : 1 = 'xxx' or 2 in ( 'xxx')..... mongodb:db.B.find({"$or":[{"x":{"$in":[' 1',' 2'...]}},{"y":"3"}]})
  3.4 $all
  {"name":jack,"age":[1,2,3]} {"name":jack,"age":[1,4,3]}
  db.B.find({"age":{"$all":[2,3]}}) :{"name":jack,"age":[1,2,3]}
  3.5 $exists
  db.B.find({"name":{"$exists":true}})   -- name db.B.find({"name":{"$exists":false}})  -- name
  3.6 null > db.C.find() { "_id" : ObjectId("5018fccd1781352fe25bf511"), "a" : "14", "b" : "14" } { "_id" : ObjectId("5018fccd1781352fe25bf512"), "a" : "15", "b" : "15" } { "_id" : ObjectId("5018fccd1781352fe25bf510"), "a" : "13", "b" : "13", "c" : null } > db.C.find({"c":null}) { "_id" : ObjectId("5018fccd1781352fe25bf511"), "a" : "14", "b" : "14" } { "_id" : ObjectId("5018fccd1781352fe25bf512"), "a" : "15", "b" : "15" } { "_id" : ObjectId("5018fccd1781352fe25bf510"), "a" : "13", "b" : "13", "c" : null } c null , c null、 c 。 c null > db.C.find({"c":{"$in":[null],"$exists":true}}) { "_id" : ObjectId("5018fccd1781352fe25bf510"), "a" : "13", "b" : "13", "c" : null }
  3.7 $not
  , , 。
  3.8 $mod
  db.B.find({"age":{"$mod":[5,1]}}) -- /5 1
  /5 1 , $not : db.B.find({"age":{"$not":{"$mod":[5,1]}}})
  3.9
  db.B.find({"name":/jack/i})
  3.10 $size
  > db.C.find() { "_id" : ObjectId("501e71557d4bd700257d8a41"), "a" : "1", "b" : [ 1, 2, 3 ] } { "_id" : ObjectId("501e71607d4bd700257d8a42"), "a" : "1", "b" : [ 1, 2 ] } > db.C.find({"b":{"$size":2}}) { "_id" : ObjectId("501e71607d4bd700257d8a42"), "a" : "1", "b" : [ 1, 2 ] }
  3.11 $slice
  , , ( )。 , 。 > db.C.find() { "_id" : ObjectId("501e71557d4bd700257d8a41"), "a" : "1", "b" : [ 1, 2, 3 ] } { "_id" : ObjectId("501e71607d4bd700257d8a42"), "a" : "1", "b" : [ 1, 2 ] } > db.C.findOne({},{"b":{"$slice":[2,3]}}) { "_id" : ObjectId("501e71557d4bd700257d8a41"), "a" : "1", "b" : [ 3 ] } > db.C.findOne({},{"b":{"$slice":-2}}) {          "_id" : ObjectId("501e71557d4bd700257d8a41"),          "a" : "1",          "b" : [                  2,                  3          ] }
  3.12 $where
  javascript 。 $where function、 。
  db.C.find({"$where":function(){return this.a == "1"}}) db.C.find({"$where":"this.a == '1'"}})
  : $where 。 BSON javascript , "$where" 。        。 , "$where" , 。
 
  4
  find , 、 、 。
  var cursor = db.C.find()     -- while(cursor.hasNext()){   var obj = cursor.next();   print(obj.a);   ...... }
  db.C.find().limit(10)  -- 10 db.C.find().skip(10)   -- 10 , 11 db.C.find().sort({"a":-1})  --sort / , ,1: ,-1:
  $maxscan:integer  -- $min:document     -- $max:document     -- $hint:document    -- $explain:boolean  -- ( 、 、 ), $snapshot:boolean -- */ ?>