MongoDB 查询$关键字 $in $or $all

时间:2019-04-13 16:20:15   收藏:0   阅读:7230

 

 

 属于:$in 
满足其中一个元素的数据
把age=13,73 的数据显示
> db.user.find({age: { $in:[13,73]}})
{ "_id" : ObjectId("5ca7a4b0219efd687462f965"), "id" : 1, "name" : "jack", "age" : 73 }
{ "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 13, "hobby" : [ "羽毛球", "篮球", "足球" ] }

 

只要满足$in [] 里面的元素 都可以查询出来

> db.user.find({hobby:{$in:["足球","篮球"]  } })
{ "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 34, "hobby" : [ "羽毛球", "篮球", "足球" ] }

> db.user.find({hobby:{$in:["羽毛球"]  } })
{ "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 34, "hobby" : [ "羽毛球", "篮球", "足球" ] }

 

 

或者:$or 
满足其中一个字段的元素数据
OR条件:
   MongoDB的OR条件语句使用了操作符$or。如:> db.collection_name.find({$or: [{key1: value1}, {key2: value2}]})

查询 name="mike" 或者 name ="jack",两个条件其中一个条件成立,都返回数据
> db.user.find({$or:[{name:"mike"},{name:"jack"}]} )
{ "_id" : ObjectId("5ca7a4b0219efd687462f965"), "id" : 1, "name" : "jack", "age" : 73 }
{ "_id" : ObjectId("5ca7a4b7219efd687462f966"), "id" : 2, "name" : "mike", "age" : 84, "gender" : "" }

 

$all:
满足所有元素的数据

符合列表里面元素条件就可以 显示数据
> db.user.find({hobby:{$all:["足球"]  } })
{ "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 34, "hobby" : [ "羽毛球", "篮球", "足球" ] }


> db.user.find({hobby:{$all:["足球","羽毛球"]  } })
{ "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 34, "hobby" : [ "羽毛球", "篮球", "足球" ] }

> db.user.find({hobby:{$all:["足球","桌球"]  } })

 

 

 

 


评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!