I have the exact same issue as described in this thread (hence the similar title): Mongoose findOneAndUpdate -- updating an object inside an array of objects
Given t
I had the same problem, so I just delete $set
from .findOneAndUpdate
Example:
function updateFood (userId) {
SavedFoods.findOneAndUpdate(
{
id: userId,
'list.id': 0
},
{
{'list.$.name': 'Something else'}
},
null,
(err) => {
if (err) {
console.log('Error:', err)
} else {
console.log('Updated', userId)
}
process.exit(0)
}
)
}
Works for me.
I think you mismatched id
and user
field
Try to change id
with user
SavedFoods.findOneAndUpdate(
{
user: userId,
'list.id': 0
}
)