Getting unique values from a collection

Today I had to get unique values from a collection and I found this method by Jonathan Pidgeon using the underscore library.
Get unique values from a collection in Meteor

var myArray = CollectionName.find().fetch();
var distinctArray = _.uniq(myArray, false, function(d) {return});
var disctinctValues = _.pluck(distinctArray, 'foo');

Since it helped me I thought it was a good idea to post it here in the forums.
Also if there is a better way to get unique values, please let me know :slight_smile:

const distinctValues = await CollectionName.rawCollection().distinct('foo');

That only works on the server, but it will handle a bazillion documents.


I thought we couldn’t use .distinct(‘foo’) in Meteor Mongo, but now I see how to!
Thank you very much for this example @robfallows

1 Like