Answer the question
In order to leave comments, you need to log in
How to update a field in an array of objects in MongoDB?
When using the $in operator on an array of objects, for some reason it only updates the first object.
Document structure:
{
_id: 'ID document',
comments: [{
_id: ObjectId("597cea075b116f0e9e2cca55"),
closed: false,
text: "string"
}, {
_id: ObjectId("597ce96f5b116f0e9e2cca54"),
closed: false,
text: "string"
}]
}
Posts
.update({
'comments._id': {
$in: commentsIds
}
},{
$set: {
'comments.$.closed': true
}
}, (e) => {
console.log(e);
process.exit();
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question