mongoose update array element code example
Example 1: mongoose modify all elements in array
db.collection.update({
"_id": "password"
},
{
"$inc": {
"waitlist.$[].queue_place": -1
}
})
Example 2: mongoose save or update
findByIdAndUpdate(_id, { something: 'updated' }, { upsert: true });
Example 3: updating an array of object in mongoose
Person.update(
{
_id: 5,
grades: { $elemMatch: { grade: { $lte: 90 }, mean: { $gt: 80 } } }
},
{ $set: { "grades.$.std" : 6 } }
)
Example 4: mongoose update array in object
Person.findOneAndUpdate({_id: id},
{
"$set": {[`items.$[outer].${propertyName}`]: value}
},
{
"arrayFilters": [{ "outer.id": itemId }]
},
function(err, response) {
...
})