DEV Community

Discussion on: Remove and modify nested documents in MongoDB

Collapse
 
rkristelijn profile image
Remi Kristelijn • Edited

I don't think you have to put the whole object, you can set individual properties of one of the elements using a double search param and place the '$set' on the field itself:

using your example above, something like:

db.yourCollection.update({
  _id: ObjectId("5b0bf696cb5dd80010bea0a3"),
  'emails._id': ObjectId("5b0bf696cb5dd80010bea0a5")
}, {'$set': {'emails.$.Email': 'w.white@break.bad'}})

Good to read that '$pull' will remove an element, note that '$push' will add an element