Updating embedded documents mongodb

Updating embedded documents mongodb

My update statement works for your sample document as included in the question description, but you'll have to modify to suit your current data.

Positional operator can match only one nested array item. From documentation at mongodb.org: As answered by Andrew Orsich, this is not possible for the moment, at least not as you wish.

I prefer to do this on the Mongo DB side, otherwise I have to load the document in client-side (Python) and construct the updated document and replace it with the existing one in Mongo DB.

UPDATE This is what I have tried and works fine IF THE Object Exists : For question #1, let's break it into two parts.

Current solution can be only load document, update as you wish and save back or multiple atomic update for each nested array id.

The default value is false, which does not insert a new document when no match is found." For multi, If set to true, updates multiple documents that meet the query criteria. @Martijn Scheffer, I don't see a way to do this as a single query anywhere in this thread.The $elem Match version does partial matching even if you have more than one field in the same element to match making it the superior method to do these things.I know it's an old answer, but I am having trouble really understanding the difference between queries made with $elem Match and their "normal" equivalent (like above).Something like: Note that this will only increment the first matched subdocument in any array (so if you have another document in the array with "item_name" equal to "my_item_two", it won't get incremented). To increment the total and the price of item_three in any document that contains "my_item_three," you can use the $inc operator on multiple fields at the same time. The answer for Question#2 is perfect and works fine :) But for Question#1, the problem is you should search for the document by "user_id", and not by "items.item_name". I felt like I was making this direction apparent from the links in my answer, but I kept getting resistance saying its not what he was looking for.In this case I can not use the Positional Operator, as I haven't specified the array in the search query. Guess the OP just needed to see examples on how to do multiple $inc.

updating embedded documents mongodb-66updating embedded documents mongodb-33updating embedded documents mongodb-51

What do you mean by "The $elem Match version does partial matching even if you have more than one field in the same element to match" ?

Join our conversation (98 Comments).
Click Here To Leave Your Comment Updating embedded documents mongodb.


  1. 1

    The awards are divided into nearly 100 categories, some of which are analogous to industry awards offered in other film and video genres and others that are specific to pornographic/erotic film and video.

  2. 1

    Successful completion of this scale will include paying attention to his heart rate, when (and if) he squeezes his inner thighs, and sensations in all parts of his body.

  3. 1

    Independence Inn is closed and Lorelai is unable to afford Dragonfly since she needs to pay Rory's university fees.

  4. 1

    If you have agreed to be exclusive, things are easy: you just need to ask him to take it down!

  5. 1

    Un véritable Roulette Chat FR ou l’on peut se taper un délire a n’importe quelle heure de la journée ou de la nuit.

Leave a Reply

Your email address will not be published. Required fields are marked *