Mongodb 如何在meteor应用程序环境中执行本机mongo方法?

Mongodb 如何在meteor应用程序环境中执行本机mongo方法?,mongodb,meteor,Mongodb,Meteor,Meteor只支持mongo集合上的几个方法,如find、findOne、insert、update、upsert、remove、allow和deny 我的问题是,我如何执行所有其他任务?我想在服务器端使用聚合,如下所示: db.eshops.aggregate([ { $unwind: '$unpairedCategories' }, { $group: { _id: '$_id', 'sum': { $sum: 1

Meteor只支持mongo集合上的几个方法,如find、findOne、insert、update、upsert、remove、allow和deny 我的问题是,我如何执行所有其他任务?我想在服务器端使用聚合,如下所示:

db.eshops.aggregate([
  {
    $unwind: '$unpairedCategories'
  }, {
    $group: {
      _id: '$_id',
      'sum': {
        $sum: 1
      }
    }
  }, {
    $group: {
      _id: null,
      total_sum: {
        '$sum': '$sum'
      }
    }
  }
]);
我是否应该将nodejs的mongodb驱动程序与meteor分开?或者meteor运行所有其他mongo收集方法的方法是什么?

其中一种方法是无处不在的数据库,即您应该能够在客户端和服务器端执行所有允许的操作(假设存在一些差异,例如客户端的
允许拒绝
规则)。我想这就是为什么你不能拥有所有mongo方法的原因:它们在minimongo上是不可行的,minimongo是你mongo集合的客户端版本

但是,如果您已准备好放弃反应性,则可以通过将其添加到服务器启动代码(),创建一个管道来处理聚合命令:

wrapAsync=(Meteor.wrapAsync)?Meteor.wrapAsync:Meteor.\u wrapAsync;
Mongo.Collection.prototype.aggregate=函数(管道){
var coll;
if(此.rawCollection){
//>=流星1.0.4
coll=this.rawCollection();
}否则{
//
如果你想保持反应性,你有两种可能的选择/解决方法

  • 创建额外的字段。基本上在集合中包含计算字段。这是一个可扩展的解决方案,不需要向服务器添加额外负载
  • 您可以使用
    cursor.Observe()
    功能,混合使用巧妙的过滤和自定义JS方法(例如sum),以获得与
    aggregate
    方法相似的结果。请注意,您可以保持反应性,但每个服务器(如果您计划在多个服务器上扩展)都需要
    Observe()
    集合。检查此示例:

  • 我发现我可以很容易地使用Mango骨料与ValuHACKS:聚合包FIY这个包使用的是代码>光标。观察< /代码>,我提到的第二个选项。我注意到你没有验证答案。如果你认为它是正确的/有帮助的,请做。
    wrapAsync = (Meteor.wrapAsync)? Meteor.wrapAsync : Meteor._wrapAsync;
    Mongo.Collection.prototype.aggregate = function(pipelines) {
      var coll;
      if (this.rawCollection) {
        // >= Meteor 1.0.4
        coll = this.rawCollection();
      } else {
        // < Meteor 1.0.4
        coll = this._getCollection();
      }
      return wrapAsync(coll.aggregate.bind(coll))(pipelines);