这里我有两个文档,我需要查询以获得title="post one"
和comments: userid="1"
,我只想获得具有上述标准的评论。不是所有的注释。
' {" _id ": ObjectId (" 53 b7f2383ed7755c2400002e "),"title":"Post One";"作者":"鲍勃","发布":ISODate("2014 - 07 - 05 t12:40:24.0z"),"页面浏览量":NumberInt (5),"评论":{" 0 ": {"标识":"1","text": "this is cool"}," 1 ": {"标识":"2","text": "这很糟糕"}," 3 ": {"标识":"3","text": "this is badexample"}" 4 ": {"标识":"4","text": "不,谢谢"}" 5 ": {"标识":"1","text": "不,很好"}" 6 ": {"标识":"1","文本":"测试"}" 7 ": {"标识":"1","text": "不,很好,还不错"}" 8 ": {"标识":"1","text": "Testing ddd"}
}}'
{
"_id": ObjectId("53b7f2383ed7755c2400002e"),
"title": "Post Two",
"author": "bob",
"posted": ISODate("2014-07-05T12:40:24.0Z"),
"pageViews": NumberInt(5),
"comments": {
"0": {
"userid": "1",
"text": "this is cool"
},
"1": {
"userid": "2",
"text": "this is bad"
},
"3": {
"userid": "3",
"text": "this is badexample"
}
"4": {
"userid": "4",
"text": "No Thanx"
}
}
}
文档中的数组可以像这样查询
db.posts.findOne({comments: { $elemMatch: { userid: 1}}, 'title':"post one"});
有关更多信息,请参阅以下问题:
- MongoDB嵌套数组查询MongoDB:查询嵌套数组