使用 Rails 和 Mongoid 对范围集合进行 Map/Reduce
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
范围被忽略,因为您直接处理集合。
您可以将 :query 选项传递给map_reduce,这将过滤文档。
例如:
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:
这就是你所追求的:
This is what you're after:
您可以在查询选项中使用 Mongoid 的范围选择器:
You can use the scope selector from Mongoid in the query option: