我有这样的文档:
{
author: "ABC1",
text:"this is a Post",
details: {
time: "14/05/2015",
Edit: "none"
},
comments: [
{
comment_text: "Hello",
user: "alan",
time:"20/05/2014 20:44"
},
{
comment_text: "Hi Every One",
user: "bob",
time:"20/05/2014 20:44"
},
{
comment_text: "Good morning , Alan",
user: "Alan",
time:"20/05/2014 20:44"
},
{
comment_text: "I'm bob",
user: "bob",
time:"20/05/2014 20:44"
},
],
category: "IT"
}
我想查询所有具有用户:"鲍勃"的子文档PHP 中的示例:db.posts.find(array("comments.user"=>"bob");
我知道这种语法对我不起作用。如果语法为真,则查找操作将显示为:
{
comment_text: "I'm bob",
user: "bob",
time:"20/05/2014 20:44"
},
{
comment_text: "Hi Every One",
user: "bob",
time:"20/05/2014 20:44"
},
如何更改文档架构来执行此操作?
Yo不需要
更改文档架构,你可以找到来自 Bob 的聚合注释:
db.posts.aggregate([
{$unwind:"$comments"},
{$project:
{"comment_text":"$comments.comment_text",
"user":"$comments.user",
"time":"$details.time",
_id:0}
},
{$match:{"user":"bob"}}
])