How can I update a property of an object that is contained in an array of a parent object using mongodb?

You need to use the Dot Notation for the arrays.

That is, you should replace the $ with the zero-based index of the element you’re trying to update.

For example:

db.users.update ({_id: '123'}, { '$set': {"friends.0.emails.0.email" : '2222'} });

will update the first email of the first friend, and

db.users.update ({_id: '123'}, { '$set': {"friends.0.emails.1.email" : '2222'} })

will update the second email of the first friend.

Leave a Comment