欢迎投稿

今日深度:

MongoDB数组查询

MongoDB数组查询


[root@sunrise16 bin]# ./mongo

MongoDB shell version: 2.0.0

 插入一个带有数组元素的文档

 查询数组元素中包含某个值的文档

> db.food.find({"fruit": {"$all": ["banana"]}})

 查询匹配指定数组的文档。注意要完全相同,数量和顺序都不能有错,否则找不到

> db.food.find({"fruit": ["banana"]})

 查询数组元素中包含多个指定值的文档,全部包含,顺序无关

> db.food.find({"fruit": {"$all": ["banana", "apple", "peach"]}})

查询包含多个值之一的文档

> db.food.find({"fruit":["banna","cherry"]})

 查询指定长度的数组。但不能查询指定长度范围的数组

> db.food.find({"fruit": {"$size": 3}})

模糊查询:

db.collname.find({"actors.name":/Catterfeld/i}, {"tag":1,"_id":1,"actors":1})

 参考推荐:

MongoDB查询(数组、内嵌文档和$where)

mongodb 常用命令

MongoDB 查询上

www.htsjk.Com true http://www.htsjk.com/DB2/20173.html NewsArticle MongoDB数组查询 [root@sunrise16 bin]# ./mongo MongoDB shell version: 2.0.0 插入一个带有数组元素的文档 查询数组元素中包含某个值的文档 db.food.find({fruit: {$all: [banana]}}) 查询匹配指定数组的文档。注...
评论暂时关闭