简体   繁体   中英

Replace nested multiple objects in mongoDB

i have this document in mongodb, and want to replace all 435 objects in all documents with new objects with key 988 and the content of object 988 is same of 435 but changing only name property, how can i do this in mongodb?

{
  item: {
    _id: ObjectId("009"),
    subItems:
    {
      "435": {
        value: "item2",
        name: "item2"
      }
    }
  },
  item: {
    _id: ObjectId("004"),
    subItems:
    {
      "123": {
        value: "item1",
        name: "item1"
      },
      "435": {
        value: "item2",
        name: "item2"
      }
    }
  }
}

Use the rename $rename

db.collection.update( {}, { $rename: { "item.subItems.435": "item.subItems.498" } } )

PS make changes according to your document structure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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