there is a document in collection books :
{ _id : someMongooseGeneratedId,
name : 'myBook',
data : [{_id : 'page', name : 'one'},{ _id : 'chapter', name : 'Chapter One' }]
}
I want to update books where book name is 'myBook' : Set name of chapter as 'Chapter 1' to the data where _id is 'chapter'
result as :
{ _id : someMongooseGeneratedId,
name : 'myBook',
data : [{_id : 'page', name : 'one'},{ _id : 'chapter', name : 'Chapter 1' }]
}
I am looking to do the same with single query with mongoose.
The following works for me :
Model.findOneAndUpdate({ name : 'myBook', "data._id" : 'chapter' }, { "data.$.name" : 'Chapter 1' });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With