I searched all stackoverflow, but didn't find anything that solves my problem.
This is my mongoose user schema:
name: String,
movies: [new Schema({
state: String,
rate: String,
note: String
})]
For example I want to update note
to test
in movie with _id: 20
here. But I don't want to send the whole movie object that contains all rate
, state
and note
values.
name: Mehdi,
movies: [
{
_id: 10,
note: 'This is awesome',
rate: 10
},
{
_id: 20,
rate: 7
}
]
This is my current code:
It overwrites the whole movie object.
User.findOneAndUpdate({
_id: user._id,
'movies._id': 20
}, {
$set: {
'movies.$': movie
}
}, {
new: true
})
Should I find
and then do it with plain JS and then .save()
it?
It's not exact question as MongoDB - Update objects in a document's array (nested updating). Because I don't want to send the whole object. I may just want to send
_id: 10,
state: 'watched'
P.S.
I found this plugin, but it has no workaround for specific object in array.