繁体   English   中英

MongoDB:在 $look 阶段后将新字段添加到现有子文档或将查找响应合并到主文档

[英]MongoDB : add New Field to existing sub document after $look stage or merge lookup response to main document

我想要来自modifieritems集合的modifierStatus子文档中的新字段“isActive”。

修改器项目集合:

{
    "_id" : ObjectId("5e6a5a0e6d40624b12453a67"),
    "modifierName" : "xxx",
    "isActive" : 1
}
{
    "_id" : ObjectId("5e6a5a0e6d40624b12453a6a"),
    "modifierName" : "yyy",
    "isActive" : 0
}

最喜欢的饮料系列:

{
  "alcoholName" : "whiskey",
  "modifierList" : [{
       "_id" : ObjectId("5e6a5a0e6d40624b12453a61"), 
       "modifierId" : ObjectId("5e6a5a0e6d40624b12453a67"),
       "modifierName" : "xxx",
     }
     {
       "_id" : ObjectId("5e6a5a0e6d40624b12453a66"),
       "modifierId" : ObjectId("5e6a5a0e6d40624b12453a6a"),
       "modifierName" : "yyy",
     }]
 }

我的查询是:

db.getCollection('favoritedrinks').aggregate([
  { "$sort": { "alcoholName": 1 } },
  {"$lookup": {
      "from": "modifieritems",
      localField: 'modifierList.modifierId', 
      foreignField: '_id', 
      as: 'modifier'
  }},
  {
     $project:{
        "alcoholName" : "$alcoholName",
        "modifierStatus":"$modifier", 
     }
   },
 ]);

但我的预期结果:

{
     "alcoholName" : "Whiskey",
      "modifierStatus" : [
        {
            "_id" : ObjectId("5e6a5a0e6d40624b12453a61"),
            "modifierId" : ObjectId("5e6a5a0e6d40624b12453a67"), 
            "modifierName" : "xxx",
            "isActive" : 1,
        },
        {
            "_id" : ObjectId("5e6a5a0e6d40624b12453a66"),
            "modifierId" : ObjectId("5e6a5a0e6d40624b12453a6a"),
            "modifierName" : "yyy",
            "isActive" : 0,
        }
    ]
}

有人请帮助我吗?

试试这个查询:

更新新要求:

db.favoritedrinks.aggregate([
  {
    "$sort": {
      "alcoholName": 1
    }
  },
  {
    "$lookup": {
      "from": "modifieritems",
      localField: "modifierList.modifierId",
      foreignField: "_id",
      as: "modifierStatus"
    }
  },
  {
    $addFields: {
      modifierStatus: {
        $map: {
          input: "$modifierList",
          as: "m",
          in: {
            $mergeObjects: [
              {
                $arrayElemAt: [ /** As filter would only get one object (cause you'll have only one matching doc in modifieritems coll for each "modifierList.modifierId", So getting first element out of array, else you need to take this array into an object & merge that field to particular object of 'modifierList') */ 
                  {
                    $filter: {
                      input: "$modifierStatus",
                      cond: {
                        $eq: [
                          "$$this._id",
                          "$$m.modifierId"
                        ]
                      }
                    }
                  },
                  0
                ]
              },
              "$$m"
            ]
          }
        }
      }
    }
  },
  {
    $project: {
      modifierStatus: 1,
      alcoholName: 1,
      _id: 0
    }
  }
])

测试: MongoDB-Playground

老的 :

db.favoritedrinks.aggregate([
    {
        "$sort": {
            "alcoholName": 1
        }
    },
    {
        $lookup: {
            from: "modifieritems",
            let: {
                id: "$modifierList.modifierId"
            },
            pipeline: [
                {
                    $match: { $expr: { $in: ["$_id", "$$id"] } }
                },
                /** Adding a new field modifierId(taken from _id field of modifieritems doc) 
                 * to each matched document from modifieritems coll */
                {
                    $addFields: {
                        modifierId: "$_id"
                    }
                }
            ],
            as: "modifierStatus"
        }
    },
    /** By mentioning 0 to particular fields to remove them & retain rest all other fields */
    {
        $project: {
            modifierList: 0,
            _id: 0
        }
    }
])

测试: MongoDB-Playground

当您希望$project包含字段的当前值同时保持相同的字段名称时,您只需指定:1 当您使用"$field"您是在明确设置该值,这将覆盖任何现有值。

尝试进行投影:

{
     $project:{
        "alcoholName" : 1,
        "modifier.isActive": 1,
        "modifier.modifierName": 1 
     }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM