Clicky

Mongo - Update nested array element

April 13, 2016

Problem:

Update a nested element inside an array, lets say, the product “availability”.
 

Example structure

Solution: 

The “non-trivial” part of this solution is the need for the “$” operator:

<![CDATA[ (…) Query query = new Query(Criteria.where(“offers.offers.seller.name”).is(offer.getSeller().getName()) .and(“offers.offers.productId”).is(offer.getProductId())); update.set(“offers.offers.$.availability”, offer.getAvailability()); (…) ]]>
SyntaxHighlighter.highlight();