$in:满足其中一个元素的数据,类似于关系型数据库中的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" : [ "羽毛球", "篮球", "足球" ] }
$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:["足球","桌球"] } })
python学习网,大量的免费MongoDB入门教程,欢迎在线学习!