簡體   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