How to replace/update the object in the array?

Hello. Tell me, is the MongoDB collection, documents with the following structure:
{
name:'Bob',
items:[{
id:12
name:'...',
lastName:'..'
},
{
id:13
name:'...',
lastName:'..'
}]
}


Needed in all documents which have the object with id = 12, replace it with another, or to update specific fields. I use moongoose, but it is possible with nodejs native driver. Tried this another way:
Model.update({ 'items.id': id }, {$set:{
'items.$[element]':obj,
 }}, {multi:true}, function(err, doc){
 console.log('doc', doc);
 });
June 10th 19 at 16:36
1 answer
June 10th 19 at 16:38
In vanilla js, but the logic should be the same to be
(currdat = "plus"
data = {name:"major",items:[{id:12,name:'extra'},
{id:13,name:'basic'}]},
function(param=12)
{return data.items[0].id==12?data.items[0].name=currdat:"not found"}()
)
//will return "plus"

Find more questions by tags MongoDBMongooseNode.js