Gui*_*ira 6 javascript mongodb mongodb-query aggregation-framework
使用聚合管道,我试图将嵌入的文档投影到根级别,而不单独投影每个字段,也不替换根级别。
例如,我想将此集合投影到根级别:
[
{
_id: "1",
city: "NY"
user: [ {
firstName: "John",
lastname: "Peters",
brothers: [
{ _id: "B1",
brotherFirstName: "Karl" }
]
}, {
firstName: "Raul",
lastname: "Other",
brothers: [
{ _id: "B2",
brotherFirstName: "May" }
]
}, {
firstName: "Paul",
lastname: "Ray",
brothers: [
{ _id: "B3",
brotherFirstName: "Bryan" }
]
}
},
{
_id: "2",
city: "NY"
user: [ {
firstName: "Joe",
lastname: "Anders",
brothers: [
{ _id: "B4",
brotherFirstName: "Carla" }
]
}, {
firstName: "Zoy",
lastname: "Bat",
brothers: [
{ _id: "B5",
brotherFirstName: "Stuart" }
]
}, {
firstName: "Ana",
lastname: "Lily",
brothers: [
{ _id: "B6",
brotherFirstName: "Carter" }
]
}
}
]
Run Code Online (Sandbox Code Playgroud)
这就是我正在寻找的:对于子文档数组中的每个文档,我想将其投影到根级别,因此对于每个“用户”,我想将其投影到“新文档”,而对于每个“兄弟”,我想将其投影到“新文件”
[{
_id: "1",
city: "NY"
firstName: "John",
lastname: "Peters",
brotherFirstName: "Karl"
}, {
_id: "1",
city: "NY"
firstName: "Raul",
lastname: "Other",
brotherFirstName: "May"
}, {
_id: "1",
city: "NY"
firstName: "Paul",
lastname: "Ray",
brotherFirstName: "Bryan"
}, {
_id: "2",
city: "NY"
firstName: "Joe",
lastname: "Anders",
brotherFirstName: "Carla"
}, {
_id: "2",
city: "NY"
firstName: "Zoy",
lastname: "Bat",
brotherFirstName: "Stuart"
}, {
_id: "2",
city: "NY"
firstName: "Ana",
lastname: "Lily",
brotherFirstName: "Carter"
}
]
Run Code Online (Sandbox Code Playgroud)
我尝试了聚合 $unwind e $replaceRoot,但我无法“替换 root”,因为我需要这些字段
db.getCollection('myColl').aggregate([ { $unwind : "$users" }, {$replaceRoot: { newRoot: "$users" }}, {$unwind : "$brothers" } ])
Run Code Online (Sandbox Code Playgroud)
编辑
运行@Anthony Winzlet 查询我有这个输出:
[
{
"_id": "B1",
"brotherFirstName": "Karl",
"city": "NY",
"firstName": "John",
"lastname": "Peters"
},
{
"_id": "B2",
"brotherFirstName": "May",
"city": "NY",
"firstName": "Raul",
"lastname": "Other"
},
{
"_id": "B3",
"brotherFirstName": "Bryan",
"city": "NY",
"firstName": "Paul",
"lastname": "Ray"
},
{
"_id": "B4",
"brotherFirstName": "Carla",
"city": "NY",
"firstName": "Joe",
"lastname": "Anders"
},
{
"_id": "B5",
"brotherFirstName": "Stuart",
"city": "NY",
"firstName": "Zoy",
"lastname": "Bat"
},
{
"_id": "B6",
"brotherFirstName": "Carter",
"city": "NY",
"firstName": "Ana",
"lastname": "Lily"
}
]
Run Code Online (Sandbox Code Playgroud)
_id 字段被兄弟的 _id 字段覆盖。我需要来自 root 的项目 _id
您需要$mergeObjects与$$ROOT文档一起使用。第一个使用 the user,第二个使用 the brother,最后一个必须使用$project来删除user和brother对象
db.collection.aggregate([
{ "$unwind": "$user" },
{ "$replaceRoot": {
"newRoot": { "$mergeObjects": ["$$ROOT", "$user"] }
}},
{ "$unwind": "$brothers" },
{ "$replaceRoot": {
"newRoot": { "$mergeObjects": ["$brothers", "$$ROOT"] }
}},
{ "$project": { "brothers": 0, "user": 0 }}
])
Run Code Online (Sandbox Code Playgroud)
[
{
"_id": "1",
"brotherFirstName": "Karl",
"city": "NY",
"firstName": "John",
"lastname": "Peters"
},
{
"_id": "1",
"brotherFirstName": "May",
"city": "NY",
"firstName": "Raul",
"lastname": "Other"
},
{
"_id": "1",
"brotherFirstName": "Bryan",
"city": "NY",
"firstName": "Paul",
"lastname": "Ray"
},
{
"_id": "2",
"brotherFirstName": "Carla",
"city": "NY",
"firstName": "Joe",
"lastname": "Anders"
},
{
"_id": "2",
"brotherFirstName": "Stuart",
"city": "NY",
"firstName": "Zoy",
"lastname": "Bat"
},
{
"_id": "2",
"brotherFirstName": "Carter",
"city": "NY",
"firstName": "Ana",
"lastname": "Lily"
}
]
Run Code Online (Sandbox Code Playgroud)