Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 398
FraK Avatar asked Dec 09 '16 15:12

FraK


1 Answers

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.

like image 96
JohnnyHK Avatar answered Oct 10 '22 14:10

JohnnyHK