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 ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With