using mongodb advanced command 3

文章来自我的博客:https://www.lwxshow.com/db/mongo/using-mongodb-advanced-command-3.html

mongodb高级命令3

#$in 在条件范围内

例:

db.cuser.find({age:{$in:[3,5]}});
{ "_id" : ObjectId("51a23348ea214a8ee3204de3"), "name" : "userid_3", "pwd" : "pwd_3", "age" : 3 }
{ "_id" : ObjectId("51a23348ea214a8ee3204de5"), "name" : "userid_5", "pwd" : "pwd_5", "age" : 5 }

#$nin不在条件范围内的

例:

db.cuser.find({age:{$nin:[3,5]}});


#并且的条件 

例:

db.cuser.find({name:"userid_2",age:2});

db.cuser.find($or:[{name:"userid_1"},{name:"userid_2"}]);
Mon May 27 01:21:22.872 JavaScript execution failed: SyntaxError: Unexpected token :
> db.cuser.find({$or:[{name:"userid_1"},{name:"userid_2"}]});
{ "_id" : ObjectId("51a23348ea214a8ee3204de2"), "name" : "userid_2", "pwd" : "pwd_2", "age" : 2 }
{ "_id" : ObjectId("51a23b98ea214a8ee3204de9"), "name" : "userid_1" }
{ "_id" : ObjectId("51a23348ea214a8ee3204de1"), "age" : 1, "name" : "userid_1", "post" : [ 1, 2, 3, 4, 5 ], "pwd" : "pwd_1" }
{ "_id" : ObjectId("51a23ce4ea214a8ee3204dea"), "name" : "userid_2" }

#or与 nor

db.cuser.find($or:[{name:"userid_1"},{name:"userid_2"}]);
Mon May 27 01:21:22.872 JavaScript execution failed: SyntaxError: Unexpected token :
> db.cuser.find({$or:[{name:"userid_1"},{name:"userid_2"}]});
{ "_id" : ObjectId("51a23348ea214a8ee3204de2"), "name" : "userid_2", "pwd" : "pwd_2", "age" : 2 }
{ "_id" : ObjectId("51a23b98ea214a8ee3204de9"), "name" : "userid_1" }
{ "_id" : ObjectId("51a23348ea214a8ee3204de1"), "age" : 1, "name" : "userid_1", "post" : [ 1, 2, 3, 4, 5 ], "pwd" : "pwd_1" }
{ "_id" : ObjectId("51a23ce4ea214a8ee3204dea"), "name" : "userid_2" }


 db.cuser.find({$or:[{name:"userid_1"},{age:2}]});
{ "_id" : ObjectId("51a23348ea214a8ee3204de2"), "name" : "userid_2", "pwd" : "pwd_2", "age" : 2 }
{ "_id" : ObjectId("51a23b98ea214a8ee3204de9"), "name" : "userid_1" }
{ "_id" : ObjectId("51a23348ea214a8ee3204de1"), "age" : 1, "name" : "userid_1", "post" : [ 1, 2, 3, 4, 5 ], "pwd" : "pwd_1" }


nor 例子

 db.cuser.find({$nor:[{name:"userid_1"},{name:"userid_2"},{name:"userid_3"}]});

{ "_id" : ObjectId("51a23348ea214a8ee3204de4"), "name" : "userid_4", "pwd" : "pwd_4", "age" : 4 }
{ "_id" : ObjectId("51a23348ea214a8ee3204de5"), "name" : "userid_5", "pwd" : "pwd_5", "age" : 5 }
{ "_id" : ObjectId("51a23348ea214a8ee3204de6"), "name" : "userid_6", "pwd" : "pwd_6", "age" : 6 }
{ "_id" : ObjectId("51a23348ea214a8ee3204de7"), "name" : "userid_7", "pwd" : "pwd_7", "age" : 7 }
{ "_id" : ObjectId("51a23348ea214a8ee3204de8"), "name" : "userid_8", "pwd" : "pwd_8", "age" : 8 }

 db.cuser.find({$nor:[{name:"userid_1"},{age:2}]});

#按长度筛选查询

db.cuser.find({post:{$size:5}});
{ "_id" : ObjectId("51a23348ea214a8ee3204de1"), "age" : 1, "name" : "userid_1", "post" : [ 1, 2, 3, 4, 5 ], "pwd" : "pwd_1" }

#正则表达式


#子对象查询

db.cuser2.find({post:{$elemMatch:{title:"titl21"}}});
{ "_id" : ObjectId("51a249f6849ea7d4e0735d37"), "name" : "userid_8", "pwd" : "pwd9", "post" : [ { "title" : "titl21", "contents" : "this 1" }, { "title" : "titl22", "contents" : "this is 2" } ] }

#null 值为null,或字段不存在

方法1:

db.cuser2.find({age:{$exists:1,$in:[null]}});
{ "_id" : ObjectId("51a24e45849ea7d4e0735d38"), "name" : "userid_5", "pwd" : "pwd5", "post" : [ { "title" : "titl5", "contents" : "this 5" }, { "title" : "titl52", "contents" : "this is 632" } ], "age" : null }
{ "_id" : ObjectId("51a24e58849ea7d4e0735d39"), "name" : "userid_4", "pwd" : "pwd6", "post" : [ { "title" : "titl6", "contents" : "this 6" }, { "title" : "titl62", "contents" : "this is 622" } ], "age" : null }


db.cuser2.find({age:{$type:10},name:"userid_4"});
{ "_id" : ObjectId("51a24e58849ea7d4e0735d39"), "name" : "userid_4", "pwd" : "pwd6", "post" : [ { "title" : "titl6", "contents" : "this 6" }, { "title" : "titl62", "contents" : "this is 622" } ], "age" : null }


方法2:

db.cuser2.find({age:{$type:10}});

你可能感兴趣的:(mongodb,增删改查,条件,lwxshow,舍得Share)