Quantcast
Channel: Mongoose, update values in array of objects - Stack Overflow
Viewing all articles
Browse latest Browse all 16

Answer by Jakub A Suplicki for Mongoose, update values in array of objects

$
0
0

Below is an example of how to update the value in the array of objects more dynamically.

Person.findOneAndUpdate({_id: id}, { "$set": {[`items.$[outer].${propertyName}`]: value} },{ "arrayFilters": [{ "outer.id": itemId }]},function(err, response) {  ...})

Note that by doing it that way, you would be able to update even deeper levels of the nested array by adding additional arrayFilters like so:

"$set": {[`items.$[outer].innerItems.$[inner].${propertyName}`]: value} "arrayFilters":[{ "outer.id": itemId },{ "inner.id": innerItemId }]

I hope it helps. Good luck!


Viewing all articles
Browse latest Browse all 16

Latest Images

Trending Articles





Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>