【问题标题】:Map/Reduce on Scoped collection with Rails and Mongoid使用 Rails 和 Mongoid 对 Scoped 集合进行 Map/Reduce
【发布时间】:2011-10-22 12:04:50
【问题描述】:

我正在尝试使用带有 Mongoid 的 MongoDB 在 Rails 3.1 应用程序中映射/缩小范围。

结果看起来很奇怪,所以我想知道 map_reduce 是否可以像这样应用于预作用域的集合:

current_user.tasks.for_year_and_month(year, month).collection.map_reduce(map, reduce, :out => "res")

for_year_and_month 限定给定月份的任务,但 map_reduce 的结果似乎也包括其他任务。现在我想知道我的 map/reduce 函数是否错误,或者 map/reduce 不能应用于预作用域的集合。

如果是这样,我必须在我的 emit 函数中完成所有作用域的工作,这会使事情变得更糟。我不敢相信。

有人能告诉我吗?

问候 费利克斯

【问题讨论】:

    标签: ruby-on-rails mongodb mapreduce mongoid


    【解决方案1】:

    范围被忽略,因为您正在直接处理集合。

    您可以将 :query 选项传递给 map_reduce,这将过滤文档。

    例如:

    Task.collection.map_reduce(map,reduce,out:{merge:'res'},query:{user_id: x, ...})
    

    【讨论】:

      【解决方案2】:

      这就是你所追求的:

      results = Task.collection.map_reduce(map, reduce, {query: {month: month, year: year}, out: "reduced_task_results"})
      filtered_results = results.find({})
      

      【讨论】:

        【解决方案3】:

        您可以在查询选项中使用来自 Mongoid 的范围选择器:

        scoped = current_user.tasks.whatever
        Task.map_reduce( ..., :query => scoped.selector)
        

        【讨论】:

          猜你喜欢
          • 2013-01-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-12-14
          • 2017-02-11
          • 1970-01-01
          相关资源
          最近更新 更多