【问题标题】:How to update field name in array in document(Mongo)?如何更新文档(Mongo)中数组中的字段名称?
【发布时间】:2021-11-16 08:34:49
【问题描述】:

MongoDB 我有用户集合。我有这样的文件:

{                                                                                                                         
   _id: ObjectId("619365c54a7d53716438933e"),                                                                              
    name: 'Chris',                                                                                                          
    hobbies: [                                                                                                                
    { title: 'Sports', frequency: 5 },                                                                                      
    { title: 'Cooking', fequency: 5 },                                                                                      
    { title: 'Hiking', frequency: 1 }                                                                                     
    ],                                                                                                                      
    isSporty: true,                                                                                                         
    cookMaster: true,                                                                                                       
    age: 35                                                                                                               }

所以,它在爱好上有数组值,在插入期间我犯了一个错误。当我插入 hobby Cooking 时,我添加了 key fequeny 而不是频率。现在我想解决这个问题。在 Mong 中存在 $rename 这样的运算符。我试过: db.users.updateMany({name:"Chris"}, {$rename: {"hobbies.fequency": "frequency"}}) 但出现错误。如何正确丰富和更改此密钥?

【问题讨论】:

    标签: mongodb mongodb-query nosql


    【解决方案1】:

    只需使用$set 进行更新,即可使用$map 更正字段名称。

    db.collection.update({},
    [
      {
        "$set": {
          "hobbies": {
            "$map": {
              "input": "$hobbies",
              "as": "h",
              "in": {
                title: "$$h.title",
                frequency: {
                  "$ifNull": [
                    "$$h.frequency",
                    "$$h.fequency"
                  ]
                }
              }
            }
          }
        }
      }
    ])
    

    这是Mongo playground 供您参考。

    【讨论】:

      【解决方案2】:
      1. $set - $map hobbies 数组字段,$mergeObject 添加新字段 frequency
      2. $unset - 在 hobbies 数组字段中删除 fequency 的字段。
      db.collection.update({
        name: "Chris"
      },
      [
        {
          $set: {
            "hobbies": {
              $map: {
                input: "$hobbies",
                in: {
                  $mergeObjects: [
                    "$$this",
                    {
                      frequency: "$$this.fequency"
                    }
                  ]
                }
              }
            }
          }
        },
        {
          $unset: "hobbies.fequency"
        }
      ])
      

      Sample Mongo Playground

      【讨论】:

        猜你喜欢
        • 2020-05-25
        • 2022-01-17
        • 1970-01-01
        • 2018-11-22
        • 2021-07-20
        • 1970-01-01
        • 1970-01-01
        • 2014-01-28
        • 1970-01-01
        相关资源
        最近更新 更多