meteor-feature-requests
meteor-feature-requests copied to clipboard
Collection.distinct support
Migrated from: meteor/meteor#5579
Check to see if a similar feature request already exists or has been closed before.
Did not find any related open / closed feature requests in this repo.
Description - excerpt from the original:
Please support collection.distinct
This could be a handy feature for pooling such unique value like userId, email address, username, etc into an array. SO case: http://stackoverflow.com/questions/33424951/meteor-fail-to-send-email-to-multiple-users/33435199
thank You...
Another use-case
Creating filter options for data tables, which are currently done manually via collection.find().fetch()
, Array.map
and _.uniq
My use case: I have many entries, each with a date. I want to create navigation at the top, one button for each date from the entries where the date is after today. With .distinct
it is easy because I can find the unique dates in the future and pass them to bootstrap to create a menu. If I have to pull the entries and filter them in javascript, I would be filtering thousands of entries on every page load just to find 6 unique dates.
@jankapunkt
for large records its slow than mongo distinct
You can do CollectionName.rawCollection().distinct(key, query, options)
It returns a promise so you can use with await/async
.
I did something like this that you might be interested in:
class SuperCollection extends Meteor.Collection {
async aggregate(pipeline = []) {
return this.rawCollection().aggregate(pipeline, { allowDiskUse: true }).toArray();
}
async pCount(query = {}, options = {}) {
return this.rawCollection().count(query, options);
}
async distinct(key = '', query = {}, options = {}) {
return this.rawCollection().distinct(key, query, options);
}
async ensureIndex(fieldOrSpec, options = {}) {
return this.rawCollection().ensureIndex(fieldOrSpec);
}
};
let result = db.tableName.aggregate([{$group: {_id: null, fieldName: {$addToSet: '$fieldName'}}}]) console.log(result[0].fieldName.sort())