Home > Enterprise >  How to update field name in array in document(Mongo)?
How to update field name in array in document(Mongo)?

Time:11-17

In MongoDB I have collection of users. There I have such document:

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

So, it has array value on hobbies and there during insert I made a mistake. When I inserted hobby Cooking, I added key fequeny instead of frequency. Now I want to fix this. In Mong exists such operator as $rename. I tried: db.users.updateMany({name:"Chris"}, {$rename: {"hobbies.fequency": "frequency"}}) But got error. How can I rich and change this key properly?

CodePudding user response:

Simply do an update with a $set to correct the field name with $map.

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

Here is the Mongo playground for your reference.

CodePudding user response:

  1. $set - $map hobbies array field, with $mergeObject to add new field frequency.
  2. $unset - Remove field for fequency in hobbies array field.
db.collection.update({
  name: "Chris"
},
[
  {
    $set: {
      "hobbies": {
        $map: {
          input: "$hobbies",
          in: {
            $mergeObjects: [
              "$$this",
              {
                frequency: "$$this.fequency"
              }
            ]
          }
        }
      }
    }
  },
  {
    $unset: "hobbies.fequency"
  }
])

Sample Mongo Playground

  • Related