Meteor 对集合有不同的查询吗? [英] Does Meteor have a distinct query for collections?

查看:17
本文介绍了Meteor 对集合有不同的查询吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的集合中返回不同的字段.我知道这些是 mongo 操作员的文档,但我对查询语言不够熟悉,无法了解如果这可能吗?

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

推荐答案

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

要使用,将以下内容放入[project]/client/lib/a.js:

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

这篇关于Meteor 对集合有不同的查询吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆