Meteor是否对集合有明确的查询?

Tim*_*Dog 11 mongodb meteor

我想在我的收藏中返回不同的字段.我知道这些是mongo运算符文档,但我对查询语言不太熟悉,知道这是否可行?

Meteor.publish("distinctCollection", function() {
    return Collection.find({ ... }, fields: { "myField": 1 });
});
Run Code Online (Sandbox Code Playgroud)

ram*_*am1 10

Collection.find({}).distinct('myField', true);
Run Code Online (Sandbox Code Playgroud)

要使用,请将以下内容放在[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);
};
Run Code Online (Sandbox Code Playgroud)

  • 在0.6.5.1之后,上述解决方案不起作用.请检查一下:https://github.com/jhoxray/meteor-mongo-extensions or package on meteorite:https://atmosphere.meteor.com/package/mongodb-aggregation (4认同)
  • 它被称为_depend,我*认为*,但它并不像我期望的那样真正地工作.不像Mongo的独特之处.我只想自己动手. (2认同)