mongodb 嵌套查询
{ 'name':{'first':'xie',
'last':'jun'
},
'age':23
}
查询整个内嵌文档如下:
1 >db.people.find({“name” : {“first” : “Joe”, “last” : “Schmoe”}}) 但需要注意这里的“name”键的值必须匹配整个文档。
只针对内嵌文档的特定键值进行查询如下:
1 >db.people.find({“name.first” : “Joe”, “name.last” : “Schmoe”}){ "content":"joe"
"comments":[
{
"author":"joe",
"score":3,
"comment":"nice post"
},
{
"author":"mary",
"score":6,
"comment":"nice post"
}
]
}
那么要查询由Joe发表的5分以上的评论,写法如下:
1 >db.blog.find({“comments” : {“$elemMatch” : {“author” : “joe”, “score” : {“$gte” : 5}}}}) 转自:http://blog.163.com/dazuiba_008/blog/static/3633498120147133647961/
页:
[1]