Zeyad Etman
Zeyad Etman

Reputation: 2560

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"
      }
    }
  }
}

Upvotes: 0

Views: 204

Answers (1)

Yahya
Yahya

Reputation: 3444

Use the rename $rename

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

PS make changes according to your document structure.

Upvotes: 1

Related Questions