Reputation: 18609
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.
Upvotes: 10
Views: 13034
Reputation: 18609
The following works for me :
Model.findOneAndUpdate({ name : 'myBook', "data._id" : 'chapter' }, { "data.$.name" : 'Chapter 1' });
Upvotes: 21