gpt4 book ai didi

javascript - 不使用 $project 将嵌套文档投影到根级别

转载 作者:可可西里 更新时间:2023-11-01 09:47:40 25 4
gpt4 key购买 nike

使用聚合管道,我试图将嵌入式文档投影到根级别,而不单独投影每个字段,也没有替换根级别。

例如,我想将这个集合投影到根级别:

[
{
_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" }
]
}
}
]

这就是我要寻找的:对于子文档数组中的每个文档,我想将其投影到根级别,因此对于每个“用户”,我都想将其投影到“新文档”,对于每个我想要的“兄弟”将其投影到“新文档”

[{
_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"
}
]

我尝试聚合 $unwind e $replaceRoot,但我不能“替换根”,因为我需要这些字段

db.getCollection('myColl').aggregate([  { $unwind : "$users" },  {$replaceRoot: { newRoot: "$users" }}, {$unwind : "$brothers" } ])

编辑

运行@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"
}
]

字段_id 被兄弟的_id 字段覆盖了。我需要来自 root 的项目 _id

最佳答案

您需要使用 $mergeObjects使用 $$ROOT 文件。首先是 user,其次是 brother,最后你必须使用 $project删除 userbrother 对象

db.collection.aggregate([
{ "$unwind": "$user" },
{ "$replaceRoot": {
"newRoot": { "$mergeObjects": ["$$ROOT", "$user"] }
}},
{ "$unwind": "$brothers" },
{ "$replaceRoot": {
"newRoot": { "$mergeObjects": ["$brothers", "$$ROOT"] }
}},
{ "$project": { "brothers": 0, "user": 0 }}
])

Output

[
{
"_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"
}
]

关于javascript - 不使用 $project 将嵌套文档投影到根级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53655826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com