How to update subdocument with findOneAndUpdate?
mongodb, mongoose, node.js
Solution
The following works for me :
Model.findOneAndUpdate({ name : 'myBook', "data._id" : 'chapter' }, { "data.$.name" : 'Chapter 1' });
Problem
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.