mongoose: MongooseArray.pull Does Not Work on Subdocuments

When using MongooseArray.pull to remove a subdocument from an array using a command such as:

someUser.attending.pull({event_id : x, user_id : y})

Mongoose will remove all subdocuments from the array that either have event_id set to x OR user_id set to y. This is unexpected since MongoDB’s $pull would remove only subdocuments that have event_id set to x AND user_id set to y.

If this is a feature, this should be clearly documented.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 16

Commits related to this issue

Most upvoted comments

There was new code added to solve the issue. Before, mongoose would not handle pull() correctly on arrays with documents that don’t have an _id. The fix for this issue was a fallback to a deep equality check (which mongodb does internally, not mongoose) when there’s no _id.

My subdocuments do not explicitly contain an _id field. For instance, I declared the UserSchema as follows:

var ListSchema = new Schema({
    user_id:    {type: mongoose.Schema.Types.ObjectId, ref:'User'},
    event_id:   {type: mongoose.Schema.Types.ObjectId, ref:'Event'}
}, {_id:false});

var UserSchema = new Schema({
    attending : {
        type : [ListSchema]
    }
});

I would also like to point out that the information you just provided about how .pull() is supposed to be used it not available on the documentation at all. For instance, how .pull() behaves with and without an _id field.