Underscore: comparing mongoose objectIds failing Underscore: comparing mongoose objectIds failing mongoose mongoose

Underscore: comparing mongoose objectIds failing


In lodash4, you can now do this:

_.intersectionWith(a, b, _.isEqual)


You can't use _.intersection with arrays of ObjectIds in this case because intersection uses simple reference equality to compare the elements, rather than comparing their contents. To compare two ObjectIds for equality you need to use the ObjectId.equals method.

So the simplest solution would be to convert the arrays to strings so that intersection will work. I know you said you tried that, but you probably had a bug somewhere as that does work.


You can use _.isEqual(object, other) to test if 2 mongoose ObjectID are have same content.

for instance,

var ret=[];_.each(b,function(x){    ret.push(_.filter(a,function(y){        return _.isEqual(x,y);    }))})

now, you should get ret contains "50dfb26223c43a501400000f" as data type of Mongoose's ObjectId.