MongoDB - 在使用 $group 进行 $lookup 后回退一个 $unwind 嵌套数组

MongoDB - Rewind an $unwind nested array after $lookup using $group

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}
            }
        ]
    ]
}

此结果有 2 个问题:

  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 的目的?

编辑:这是原文post,上下文:

因为 $lookup operator produces an array field, you need to $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()