MrSandman MrSandman - 27 days ago 8
Node.js Question

mongoose update object in array with a specific key

so I have this mongoose schema:

schema = mongoose.Schema({
identifier: Number,
shopItems: [{
identifier: Number,
price: Number

now I know how to push new items to this collections shopItems array with
{ $push { shopItems { identifier: id, price: price } }

But now I want to update an item in the shopItems array with a specific identifier value, is that possible?

I was thinking that this would work:
{ $push { "shopItems.identifier": myVal, price: newPrice } }
but it didnt, so I am really lost.

Answer Source

You can achieve this as follows:

db.collection.update({'shopItems.identifier' : myVal }, { $set : { 'shopItems.$.price' : newPrice}});