gpt4 book ai didi

MongoDB - 在 $lookup 之后使用 $group 倒回 $unwind 嵌套数组

转载 作者:IT老高 更新时间:2023-10-28 13:14:56 26 4
gpt4 key购买 nike

MongoDB 聚合每分钟都在指数级地复杂化!

我目前是 $unwind 一个嵌套数组,然后通过 _id 中的每个对象执行 $lookup展开的嵌套数组。我最后的尝试是用 $group 来逆转展开。但是,我无法重建原始嵌入数组及其原始属性名称,以及每个文档的其余原始直接属性。

这是我目前的尝试:

db.users.aggregate([
{
$unwind: "$profile",
$unwind: {
path: "$profile.universities",
preserveNullAndEmptyArrays: true
}
},
{
$lookup: {
from: "universities",
localField: "profile.universities._id",
foreignField: "_id",
as: "profile.universities"
}
},
{
$group: {
_id: "$_id",
emails: { "$first": "$emails" },
profile: { "$first": "$profile" },
universities: { "$push": "$profile.universities" }
}
}
]).pretty()

我得到的是这样的:

{
"_id" : "A_USER_ID",
"emails" : [
{
"address" : "AN_EMAIL_ADDRESS",
"verified" : false
}
],
"profile" : {
"name" : "NAME",
"company" : "A COMPANY",
"title" : "A TITLE",
"phone" : "123-123-1234",
"disabled" : false,
"universities" : [
{
"_id" : "ID_1",
"name" : "UNIVERSITY_NAME_1",
"code" : "CODE_1",
"styles" : {AN_OBJECT}
}
]
},
"universities" : [
[
{
"_id" : "ID_1",
"name" : "UNIVERSITY_NAME_1",
"code" : "CODE_1",
"styles" : {AN_OBJECT}
}
],
[
{
"_id" : "ID_2",
"name" : "UNIVERSITY_NAME_2",
"code" : "CODE_2",
"styles" : {AN_OBJECT}
}
]
]
}

这个结果有两个问题:

  1. 生成的 universities 是一组数组,每个数组包含一个对象,因为 $lookup 为原始 $profile.universities< 返回了单个元素数组 嵌套数组。它应该只是一个对象数组。
  2. 生成的 universities 应该在嵌套在 profiles 下的原始位置。我知道为什么原来的 profile.universities 是这样的,因为我使用的是 $first 运算符。我的意图是保留 profile 的所有原始属性,同时保留原始嵌套 universities 数组。

最终,我需要的是这样的:

{
"_id" : "A_USER_ID",
"emails" : [
{
"address" : "AN_EMAIL_ADDRESS",
"verified" : false
}
],
"profile" : {
"name" : "NAME",
"company" : "A COMPANY",
"title" : "A TITLE",
"phone" : "123-123-1234",
"disabled" : false,
"universities" : [
{
"_id" : "ID_1",
"name" : "UNIVERSITY_NAME_1",
"code" : "CODE_1",
"styles" : {AN_OBJECT}
},
{
"_id" : "ID_2",
"name" : "UNIVERSITY_NAME_2",
"code" : "CODE_2",
"styles" : {AN_OBJECT}
}
]
}
}

我可以使用另一个运算符来代替 $group 来实现此目的吗?还是我错误地理解了 $group 的目的?

编辑:这是原始帖子,用于上下文: If Mongo $lookup is a left outer join, then how come it excludes non-matching documents?

最佳答案

因为 $lookup 运算符产生一个数组字段,你需要 $unwind $group 管道之前的新字段以获得所需的结果:

db.users.aggregate([
{ "$unwind": "$profile" },
{ "$unwind": {
"path": "$profile.universities",
"preserveNullAndEmptyArrays": true
} },
{ "$lookup": {
"from": "universities",
"localField": "profile.universities._id",
"foreignField": "_id",
"as": "universities"
} },
{ "$unwind": "$universities" },
{ "$group": {
"_id": "$_id",
"emails": { "$first": "$emails" },
"profile": { "$first": "$profile" },
"universities": { "$push": "$universities" }
} },
{ "$project": {
"emails": 1,
"profile.name" : 1,
"profile.company": 1,
"profile.title" : 1,
"profile.phone" : 1,
"profile.disabled": 1,
"profile.universities": "$universities"
} }
]).pretty()

关于MongoDB - 在 $lookup 之后使用 $group 倒回 $unwind 嵌套数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39491447/

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