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.
identifierto identify its position inshopItemsarray ? If yes you can use positional operator to update theidentifierat that position with new element. Something like find and replace in array.db.collection_name.update({"shopItems.identifier": id}, {"$set":{"shopItems.$":{identifier:myVal, price: newPrice }}})db.collection_name.update({"shopItems.identifier": myVal}, {"$set":{"shopItems.$.price: newPrice }})