I\'m using Meteor, and I\'m trying to find only distinct (unique) values of a field. Mongodb has the command
Collection.distinct(\"fieldname\");
Improving richsilv's answer:
function distinct(collection, field) { return _.uniq(collection.find({}, { sort: {[field]: 1}, fields: {[field]: 1} }).fetch().map(x => x[field]), true); }
How to use:
var arrResults = distinct(MyCollection, 'myfield');