如何从以下BSON文档中获取子文档值
{
"_id" : ObjectId("52415e45ccf279207b91a2a0"),
"created" : ISODate("2013-09-24T09:41:25.760Z"),
"creator" : "52415e45ccf279207b91a29e",
"description" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
"lastModif" : ISODate("2013-09-24T09:41:25.760Z"),
"title" : "My post title",
"comments" : [
{
"title" : "Comment 1",
"content" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
"created" : ISODate("2013-09-24T09:41:25.764Z"),
"lastModif" : ISODate("2013-09-24T09:41:25.764Z"),
"creator" : "52415e45ccf279207b91a2a3",
"_id" : ObjectId("52415e45ccf279207b91a2a1")
},
{
"title" : "Comment 2",
"content" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
"created" : ISODate("2013-09-24T09:41:25.764Z"),
"lastModif" : ISODate("2013-09-24T09:41:25.764Z"),
"creator" : "52415e45ccf279207b91a2a3",
"_id" : ObjectId("52415e45ccf279207b91a29f")
},
{
"title" : "Comment 3",
"content" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
"created" : ISODate("2013-09-24T09:41:25.764Z"),
"lastModif" : ISODate("2013-09-24T09:41:25.764Z"),
"creator" : "52415e45ccf279207b91a2a3",
"_id" : ObjectId("52415e45ccf279207b91a287")
}
...
]
}
我需要获取title='Comment 1'
。
任何人都可以帮助我。
答案 0 :(得分:0)
如果您想查找包含 title =“Comment 1”的评论的文档,那么您可以使用:
db.test.find({"comments.title" : "Comment 1"})
如果您还希望投影结果以便它只显示子文档,那么您应该按如下方式更新查询:
db.test.find({"comments.title" : "Comment 1"},{comments : {$elemMatch : {title:"Comment 1"}}})