I have a users collection and every user has a number of contacts. When a user deletes their account I want that this user's id would be deleted from the contacts array of all the users with who this user is connected. I have tried this Model.Update query but it doesn't work. Here is my code so far:
User.update({'userId':{ $in: userIds },
$pullAll: {'contacts': [myId] },'multi': true
},function(err, count) {
if(err){
console.log(err);
}else{
console.log(count);
}
});
The update document and multi should be passed as separate arguments:
User.update({
userId : { $in : userIds } // conditions
}, {
$pullAll : { contacts : [myId] } // document
}, {
multi : true // options
}, function(err, count) {
if (err) {
console.log(err);
} else {
console.log(count);
}
});
Documentation here.
Can update multiple documents with multiple conditions
Model.update({
_id : { $in : ids} // conditions
}, {
$set: {deletion_indicator: constants.N} // document
}, {
multi : true // options
}, function(err, result) {
if (err) {
console.log(err);
} else {
console.log(result);
}
});