0

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

1 Answer 1

1

Use the rename $rename

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

PS make changes according to your document structure.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.