屬於:$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:["足球","桌球"] } })
