簡體   English   中英

如何將 MongoDB 文檔字段移動到對象數組?

[英]How to move MongoDB document fields to an array of objects?

給定類似於以下文檔的文檔集合

{
    "_id": {
        "$oid": "60582f08bf1d636f4b762ebc"
    }
    "experience": [{
        "title": "Senior Customer Success Account Manager",
        "company": "Microsoft",
        "tenure": 8
    }, {
        "title": "Senior Service Delivery Manager",
        "company": "Microsoft",
        "tenure": 34
    }],
    "company3": "Oracle",
    "tenure3": 10,
    "title3": "Senior Customer Success Manager - EMEA |Oracle Marketing Cloud"
}

我將如何編寫 updateMany 或其他 shell 命令以將體驗數組中的 company3、tenure3 和 title3 作為新的 object {company: <company3 value>, title: <title3 value>, tenure: <tenure3 value>}

好像您正在尋找此聚合更新:

db.collection.update({},
[
  {
    $set: {
      experience: {
        $concatArrays: [
          "$experience",
          [
            {
              company: "$company3",
              title: "$title3",
              tenure: "$tenure3"
            }
          ]
        ]
      }
    }
  },
  {
    $unset: "company3"
  },
  {
    $unset: "tenure3"
  },
  {
    $unset: "title3"
  }
],
{
  multi: true
})

游樂場: https://mongoplayground.net/p/xoEveE0rdBN

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM