meteor-feature-requests icon indicating copy to clipboard operation
meteor-feature-requests copied to clipboard

Collection.distinct support

Open jankapunkt opened this issue 7 years ago • 5 comments

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...

jankapunkt avatar Jul 27 '17 14:07 jankapunkt

Another use-case Creating filter options for data tables, which are currently done manually via collection.find().fetch(), Array.map and _.uniq

jankapunkt avatar Jul 27 '17 14:07 jankapunkt

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.

btorresgil avatar Aug 14 '17 04:08 btorresgil

@jankapunkt

for large records its slow than mongo distinct

crapthings avatar Sep 30 '17 15:09 crapthings

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);
  }
};

bmanturner avatar Oct 17 '17 19:10 bmanturner

let result = db.tableName.aggregate([{$group: {_id: null, fieldName: {$addToSet: '$fieldName'}}}]) console.log(result[0].fieldName.sort())

boostbob avatar Nov 04 '19 02:11 boostbob