1

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);
                        } 
                    });
2
  • I take it that userIds is an array of id's for users that need to be removed and subsequently remove them from the contacts array of the other users, correct? Can you also update your question to show the User model's schema definition? Commented Apr 25, 2017 at 14:17
  • @chridam yes, correct. Commented Apr 25, 2017 at 14:21

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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);
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.