2015-04-03 139 views
2

我是MongoDB的新手。我想根据嵌套文档的条件检索文档。 例子:MongoDB find()查询嵌套文档

db.user.insert([ 
{name:"abc",uid:"abc.b",pass:"abc3",follows:500,posts:78}, 
{name:"xyz",uid:"xyz.r",pass:"xyz4",follows:600,posts:78}, 
{name:"pqr",uid:"pqr.s",pass:"pqr5",follows:600,posts:78, 
     comments:[{ 
        uid:"abc.b",msg:"great"},{uid:"xyz.r",msg:"awesome"} 
       ]} 
]) 

在这里,如何检索基于条件等同为RDBMS文件:

select * where comments.user="abc"; 

通过它我可以用 “ABC” 评论的用户。

在此先感谢。

回答

2

使用dot notation访问内嵌的数组元素在你的查询对象。例如,下面将为user收集的comments阵列的uid领域的文件查询与价值"abc.d"

db.user.find({"comments.uid": "abc.b"}); 
-1
db.user.find({"comments.user"="abc"}) 
+1

这一个是错的。 '='与MongoDB中的条件查询无关 – 2015-04-03 20:29:05