Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
944 views
in Technique[技术] by (71.8m points)

mongodb - Does Meteor have a distinct query for collections?

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 possible?

Meteor.publish("distinctCollection", function() {
    return Collection.find({ ... }, fields: { "myField": 1 });
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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);
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...