Lez Yeoh February 2016

Difference between two arrays of Mongo Document _ids

Essentially, I have two arrays of _ids from Mongodb that I'm trying to compare/get the differences of.

Array 1 comes directly from a document.find() call. Array 2 comes from an document.Aggregate call, of which I then _.pluck the _id from both of them.

Array 1 might look like [ 1 , 57, 58, 100 ] as an example.
Array 2 might look like [ 1 , 2, 3, 4, 55, 100 ] as an example.

I want to find the difference and return and [ 57, 58 ].

To do this I've been using lodash as the library. And I've tried the following two methods:

_.difference(array1, array2)

and

_.filter(array1, function(obj){ return !_.findWhere(array2, obj); });

But both don't seem to work. I'd love some help if possible.

Answers


Lez Yeoh February 2016

Ignore. Turns out the filter code worked. It was my db with the error.

_.filter(array1, function(obj){ return !_.findWhere(array2, obj); });

Post Status

Asked in February 2016
Viewed 3,237 times
Voted 8
Answered 1 times

Search




Leave an answer