我的结构如下所示:
{
field1: "somevalue",
name:"xtz",
nested_documents: [
{
x:1,
y:2,
info:[
{name:"sachin",value:"test"},
{name:"sachin", value:"test"}
]
},
{
x:1,
y:3,
info:[
{name:"sachin1",value:"test"},
{name:"sachin2", value:"test"}
]
},
{
x:4,
y:3,
info:[
{name:"sachin",value:"test"},
{name:"sachin", value:"test"}
]
}
]
}
我知道我可以使用下面的代码检索第一个数组中的元素:
db.test.find({"nested_documents.x": 1},{_id: 0, nested_documents: {$elemMatch: {x: 1}}}
但是,我想为name
属性应用相同的逻辑。
我想只检索名为`sachin'的文档。
我尝试过的内容如下所示:
db.test.find({"nested_documents.info.name": "sachin"},
{_id: 0, 'nested_documents.info': {$elemMatch: {name: "sachin"}}});
但Mongo db说它不支持'。'投影内部投影:(。
有没有其他方法可以做到这一点?(使用命令提示符或代码)
插入文档的命令如下所示:
db.test.insert( {
field1: "somevalue",
name:"xtz",
nested_documents: [
{
x:1,
y:2,
info:[
{name:"sachin",value:"test"},
{name:"sachin", value:"test"}
]
},
{
x:1,
y:3,
info:[
{name:"sachin1",value:"test"},
{name:"sachin2", value:"test"}
]
},
{
x:4,
y:3,
info:[
{name:"sachin",value:"test"},
{name:"sachin", value:"test"}
]
}
]
}
)
我期待输出为:
{ "_id" : ObjectId("5142e0f153cd2aab3a3bae5b"),
"nested_documents" : [
{ "x" : 1, "y" : 2,
"info" : [
{ "name" : "sachin", "value" : "test" },
{ "name" : "sachin", "value" : "test" }
]
},
{ "x" : 4, "y" : 3,
"info" : [ { "name" : "sachin", "value" : "test" },
{ "name" : "sachin", "value" : "test" }
]
}
]
}
答案 0 :(得分:0)
你必须使用带有双$展开的aggregate(),如下所示:
db.test.aggregate([
// filter for documents with x=1
// note: this will use an index, if defined on "nested_documents.x"
//{ $match: { "nested_documents.x": 1 } },
// reduce data to nested_documents, as other fields are not considered
{ $project: { nested_documents: 1 } },
// flatten the outer array
{ $unwind: "$nested_documents" },
// filter for nested_documents with x=1
// note that at this point nested_documents is no longer an array
//{ $match: { "nested_documents.x": 1 } },
// flatten the inner array
{ $unwind: "$nested_documents.info" },
// filter for nested_documents.info.name = "sachin"
// note that at this point nested_documents.info is no longer an array
{ $match: { "nested_documents.info.name": "sachin" } },
// format output: re-create inner array
{ $group: { _id: { id: "$_id",
nested_documents: {
x: "$nested_documents.x",
y: "$nested_documents.y"
}
},
info: { $push: "$nested_documents.info" } } },
{ $project: { "nested_documents.x": "$_id.nested_documents.x",
"nested_documents.y": "$_id.nested_documents.y",
"nested_documents.info": "$info" } },
// format output: re-create outer array
{ $group: { _id: "$_id.id", nested_documents: { $push: "$nested_documents" } } },
])
注意:我输入//注释过滤x = 1的逻辑,如前面的例子所示
结果是:
{
"result" : [
{
"_id" : ObjectId("515d873457a0887a97cc8d19"),
"nested_documents" : [
{
"x" : 4,
"y" : 3,
"info" : [
{
"name" : "sachin",
"value" : "test"
},
{
"name" : "sachin",
"value" : "test"
}
]
},
{
"x" : 1,
"y" : 2,
"info" : [
{
"name" : "sachin",
"value" : "test"
},
{
"name" : "sachin",
"value" : "test"
}
]
}
]
}
],
"ok" : 1
}
有关聚合的更多信息,请参阅http://docs.mongodb.org/manual/applications/aggregation/