Ruby on rails 在有条件的MongoDb集合上运行map/reduce

Ruby on rails 在有条件的MongoDb集合上运行map/reduce,ruby-on-rails,mongodb,mapreduce,Ruby On Rails,Mongodb,Mapreduce,我对map/reduce的理解似乎不够。我想知道是否可以从集合中选择文档的子集,并仅在该子集上运行map和reduce函数 一般来说,这是可能的吗 如果不是,这意味着在发出之前,所有过滤都必须在map函数中完成。我已经为我的用例写了一张这样的地图: map = <<-EOF function(){ Array.prototype.contains = function(obj) { var i = this.length; while (i--)

我对map/reduce的理解似乎不够。我想知道是否可以从集合中选择文档的子集,并仅在该子集上运行map和reduce函数

一般来说,这是可能的吗

如果不是,这意味着在发出之前,所有过滤都必须在map函数中完成。我已经为我的用例写了一张这样的地图:

map = <<-EOF
  function(){
    Array.prototype.contains = function(obj) {
      var i = this.length;
      while (i--) {
        if (this[i] === obj) {
          return true;
        }
      }
      return false;
    };

    project_ids = ['#{@project_ids.map{|pid| pid.to_s}.join('\',\'')}'];

    if(project_ids.contains(this.project_id.toString())) {
      if(this.time.getFullYear() == '#{@year}' && this.time.getMonth() == '#{@month.to_i - 1}') {
        emit(
          this.time.getDate(),
          {
            foos: this.stats.foos
          }
        );
      }
    }
  };
EOF

map=有。map/reduce函数的一个参数是“query”,它精确地允许您需要什么。查看详细信息。

如果您使用的是ORM,那么您通常可以从作用域集合中获取选择器,并在map reduce中使用该选择器,例如使用Mongoid:

scoped = Project.active.this_year
Project.collection.map_reduce( ..., :query => scoped.selector)

雷蒙,多谢。事实上,这正是我想要的。