FraK
FraK

Reputation: 1089

Mongoose indexOf in an ObjectId array

I have the following schema:

var daySchema = new Schema({
date: {
    type: Date,
    required: true
},
activities: [{
    type: Schema.Types.ObjectId,
    ref: 'Activity'
}]
});

And I am removing an activity within the daySchema:

var index = day.activities.indexOf("584aa9c16791eb1ec4ad8e73");
day.activities.splice(index, 1);

Can someone explain me why this works ?. The array "activities" is an array of "ObjectId". So the "indexOf" should not work with objects, but still the "indexOf" is able to find the element based on the id. This is driving me crazy, is something else going on here, maybe a map function inside ObjectId ?

Upvotes: 5

Views: 6533

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 311935

This works because Mongoose wraps an array in MongooseArray which provides its own indexOf method which supports this string-based comparison rather than the strict equality test used by the native array implementation.

Upvotes: 9

Related Questions