Does Meteor have a distinct query for collections?

前端 未结 1 1245
再見小時候
再見小時候 2020-12-09 11:44

I\'d like to return distinct fields in my collection. I know these are the docs for mongo operators, but I\'m not familiar enough with the query language to know if this is

1条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-09 12:23

    Collection.find({}).distinct('myField', true);
    

    To use, put the following in [project]/client/lib/a.js:

    LocalCollection.Cursor.prototype.distinct = function (key,random) {
      var self = this;
    
      if (self.db_objects === null)
        self.db_objects = self._getRawObjects(true);
      if (random)
        self.db_objects = _.shuffle(self.db_objects);
      if (self.reactive)
        self._markAsReactive({ordered: true,
                              added: true,
                              removed: true,
                              changed: true,
                              moved: true});
      var res = {};
      _.each(self.db_objects,function(value){
    
        if(!res[value[key]]){
            res[value[key]] = value;
        }
      });
      return _.values(res);
    };
    

    0 讨论(0)
提交回复
热议问题