Underscore: comparing mongoose objectIds failing
You can't use _.intersection
with arrays of ObjectId
s in this case because intersection
uses simple reference equality to compare the elements, rather than comparing their contents. To compare two ObjectId
s 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.