【MongoDB学习笔记17】MongoDB的查询:find中的数组查询
db.post.find() { "_id" : ObjectId("54a530c3ff0df3732bac1681"), "id" : 2, "name" : "joe", "age" : 30, "sex" : 1, "school" : "marry" } { "_id" : ObjectId("54a530c3ff0df3732bac1680"), "id" : 1, "name" : "joe", "age" : 30, "comments" : [ "test2", "test9", "test5" ], "sex" : 1, "school" : "marry" } { "_id" : ObjectId("54a9700e1b5afd45354fd086"), "id" : 3, "test3" : 3 } { "_id" : ObjectId("54a9701c1b5afd45354fd087"), "id" : 4, "test4" : 4 } { "_id" : ObjectId("54a970281b5afd45354fd088"), "id" : 5, "test5" : 5 } { "_id" : ObjectId("54a970351b5afd45354fd089"), "id" : 6, "test6" : 6 } { "_id" : ObjectId("54a970781b5afd45354fd08a"), "id" : 7, "test7" : 7 } { "_id" : ObjectId("54a970831b5afd45354fd08b"), "id" : 8, "test8" : 8 } { "_id" : ObjectId("54a970901b5afd45354fd08c"), "id" : 9, "test9" : 9 } { "_id" : ObjectId("54a9709c1b5afd45354fd08d"), "id" : 10, "test10" : 10 } { "_id" : ObjectId("54aa8a90652d8bdfa0566d34"), "id" : 11, "test10" : 11 } { "_id" : ObjectId("54aa97b894dcf31069b590ca"), "id" : 12, "fruit" : [ "apple", "banana", "peach" ] } { "_id" : ObjectId("54aa97d794dcf31069b590cb"), "id" : 13, "fruit" : [ "apple", "kumquat", "orange" ] } { "_id" : ObjectId("54aa97f294dcf31069b590cc"), "id" : 14, "fruit" : [ "cherry", "banana", "apple" ] } >
成都创新互联是专业的普兰店网站建设公司,普兰店接单;提供网站建设、成都网站设计,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行普兰店网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
一、$all来匹配数组中的一组元素
> db.post.find({"fruit":{$all:["apple","banana"]}}) { "_id" : ObjectId("54aa97b894dcf31069b590ca"), "id" : 12, "fruit" : [ "apple", "banana", "peach" ] } { "_id" : ObjectId("54aa97f294dcf31069b590cc"), "id" : 14, "fruit" : [ "cherry", "banana", "apple" ] } >
可以看到apple和banana的顺序无关系;
二、$size来查询匹配的数组长度;
> db.post.find({},{"id":1,"fruit":1,"_id":0}) { "id" : 2 } { "id" : 1 } { "id" : 3 } { "id" : 4 } { "id" : 5 } { "id" : 6 } { "id" : 7 } { "id" : 8 } { "id" : 9 } { "id" : 10 } { "id" : 11 } { "id" : 12, "fruit" : [ "apple", "banana", "peach" ] } { "id" : 13, "fruit" : [ "apple", "kumquat", "orange", "fruit01" ] } { } > > db.post.find({"fruit":{$size:3}}) { "_id" : ObjectId("54aa97b894dcf31069b590ca"), "id" : 12, "fruit" : [ "apple", "banana", "peach" ] } >
三、$slice操作符
> db.post.find({"id":13}) { "_id" : ObjectId("54aa97d794dcf31069b590cb"), "id" : 13, "fruit" : [ "apple", "kumquat", "orange", "fruit01" ] } >
返回数组前两个值
> db.post.find({"id":13},{"fruit":{$slice:2}}) { "_id" : ObjectId("54aa97d794dcf31069b590cb"), "id" : 13, "fruit" : [ "apple", "kumquat" ] } >
返回数组的后两个值
> db.post.find({"id":13},{"fruit":{$slice:-2}}) { "_id" : ObjectId("54aa97d794dcf31069b590cb"), "id" : 13, "fruit" : [ "orange", "fruit01" ] } >
返回数组中间的两个值
> db.post.find({"id":13},{"fruit":{$slice:[1,2]}}) { "_id" : ObjectId("54aa97d794dcf31069b590cb"), "id" : 13, "fruit" : [ "kumquat", "orange" ] }
四、利用数组下表查询
> db.post.find({"fruit.3":"fruit01"}) { "_id" : ObjectId("54aa97d794dcf31069b590cb"), "id" : 13, "fruit" : [ "apple", "kumquat", "orange", "fruit01" ] } >
本文题目:【MongoDB学习笔记17】MongoDB的查询:find中的数组查询
分享链接:http://azwzsj.com/article/gjeepg.html