有条件地在 MongoDb 集合上运行 map/reduce

发布于 2024-12-11 17:09:40 字数 920 浏览 0 评论 0原文

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

一般情况下可以吗?

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

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

这是 Rails 项目的一部分,使用 map/reduce 实现的方法实际上比纯 ruby​​ 方法慢 3 倍。

所以我想知道是否有可能首先应用某些条件过滤我的记录集,然后通过映射/减少运行记录子集来获取我的数据。

谁能启发我吗? 提前谢谢 菲利克斯

My understanding of map/reduce seems to be insufficient. I wonder if I can select a subset of documents from a collection and run my map and reduce functions only on that subset.

Is that possible in general?

In case not, it meant, that all filtering has to be done in the map function before emitting. I already wrote a map like that for my usecase:

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

This is part of a Rails project and the method implemented using map / reduce is in fact 3 times slower than a pure ruby method.

So I wonder whether there is any possibility to first filter my set of records applying some conditions and afterwards run the subset of records through map / reduce to get my data.

Can anyone enlighten me?
Thx in advance
Felix

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

┼── 2024-12-18 17:09:40

有。 Map/Reduce 函数的参数之一是“查询”,它可以满足您的需求。看看 http://www.mongodb.org/display/DOCS/MapReduce了解详情。

There is. One of the parameters of the map/reduce function is "query" which allows exactly what you need. Have a look at http://www.mongodb.org/display/DOCS/MapReduce for details.

旧情勿念 2024-12-18 17:09:40

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

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

If you're using an ORM, then you can often obtain the selector from a scoped collection and use that in the map-reduce, e.g. with Mongoid:

scoped = Project.active.this_year
Project.collection.map_reduce( ..., :query => scoped.selector)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文