使用 Rails 和 Mongoid 对范围集合进行 Map/Reduce

发布于 2024-12-11 05:55:50 字数 442 浏览 0 评论 0原文

我正在尝试使用 MongoDB 和 Mongoid 在 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不能应用于预范围集合。

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

谁能启发我吗?

问候 菲利克斯

I am trying to map/reduce a scope in a Rails 3.1 app using MongoDB with Mongoid.

The results seem odd, so I wonder if map_reduce can be applied on a prescoped collection for example like that:

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

for_year_and_month scopes tasks on a given month, but the results from map_reduce seem to include other tasks too. Now I wonder whether my map/reduce functions are wrong, or map/reduce can not be applied on pre-scoped collections.

If so, I had to do all the scope's work in my emit function what would make things even worse. I can't believe that.

Can anyone enlighten me?

Regards
Felix

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

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

发布评论

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

评论(3

猥琐帝 2024-12-18 05:55:50

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

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

例如:

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

The scope is ignored because you're working directly on collection.

You can pass the :query option to map_reduce and this will filter documents.

Eg:

Task.collection.map_reduce(map,reduce,out:{merge:'res'},query:{user_id: x, ...})
我很坚强 2024-12-18 05:55:50

这就是你所追求的:

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

This is what you're after:

results = Task.collection.map_reduce(map, reduce, {query: {month: month, year: year}, out: "reduced_task_results"})
filtered_results = results.find({})
情丝乱 2024-12-18 05:55:50

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

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

You can use the scope selector from Mongoid in the query option:

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