0

I'm working with Mongoose for the first time and I'm trying to accomplish what seems to be a simple task. I have a users document that contains a clients property, which consists of an array of client id's. As an example, my document looks like this:

{
  email: "[email protected]",
  password: "$2a$10$xZVzMYgyoyT.biOMDrBlRe3HNHY5A6lXga6uc8b/cnIAX/khQ7ep2",
  modified: ISODate("2013-01-16T00:13:56.894Z"),
  created: ISODate("2013-01-16T00:13:56.894Z"),
  _id: ObjectId("50f5f0c4d6bbbcc6ce000002"),
  clients: [
    "50f6e118e0ccf9a1e9000001",
    "50f6e12be0ccf9a1e9000002"
  ],
  __v: 0
}

I've created a middleware that removes dependencies for a client when I call remove();

clientSchema.pre('remove', function(next) {

    Sweepstakes.remove({client_id: this._id}).exec();
    Submission.remove({client_id: this._id}).exec();

    // find users with this._id present in clients, and remove from array

    next();

});

Now, I simply need to locate all users who have the client id present in clients, remove the id, and update the user.

I know that I could easily query for all users with the id present, and then loop through and save out each user individually... But that seems inefficient, and my gut tells me that there is a better way to accomplish what I am trying to do -- just having a hard time locating it in the documentation.

What is the most efficient way to do this using Mongoose?

2
  • 1
    Are you aware of this handy operator reference in the documentation? Commented Jan 16, 2013 at 17:43
  • @JohnnyHK I do now! Thanks for sending this my way. Commented Jan 16, 2013 at 17:58

1 Answer 1

2

Probably something like this:

Users.update({condition}, {$pull : { clients: this._id} }, function(err, numAffected) {
    //handle errors and whatever
    next();
});

You can add clients : {$in : [this._id]} as condition to limit the update.

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

3 Comments

I gave that a shot, and it's not throwing any errors. Coming back as 0 rows affected, and the id is not being removed from the clients array. I double checked the documentation that JohhnyHK provided above and it looks like my condition is set up properly. Any chance you can look at my modified code and see what/if I'm missing anything? GIST
@NickParsons looks fine to me. I would recommend putting protective braces around even single-statement if branches though,
I narrowed it down. It appears that the ids inside of the clients array have to be of type ObjectId. I was manually putting them into my clients array as strings. There's something going on with my schema or create function that's not allowing them to be casted as ObjectIds. The debugging begins.

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.