在MongoDB中限制和排序内联Map/Reduce [英] Limit and sort inline Map/Reduce in MongoDB

查看:92
本文介绍了在MongoDB中限制和排序内联Map/Reduce的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,我无法在Mongo中指定复杂的排序函数,因此我不得不使用map/reduce查询,因​​为我什至没有使用reduce,所以感觉有点没用.很好,但是它返回的集合太大,我需要对其进行排序和限制(分页).可以通过内联调用来做到这一点吗?我无法存储结果集合,然后再对其进行查询,因为该集合对于每个用户而言都是唯一的,并且会不断变化.

As far as I know, I can't specify a complex sort function in Mongo so I have to use a map/reduce query which feels a bit useless since I don't even use the reduce portion. This is fine but the collection it returns is too large, I need to sort and limit it (paginate). Is it possible to do that with an inline call? I can't store the resulting collection and then query it because the collection is unique to each user and constantly changing.

推荐答案

通常在mongodb mapreduce中sort在映射之前应用,实际上是将它们应用于输入数据而不是输出.

Normally sort in mongodb mapreduce applied before the map, literally they are applied to the input data not the output.

来自mongodb文档

From mongodb docs

   [, sort : <sorts the input objects using this key. Useful for optimization, like sorting by the emit key for fewer reduces>]
   [, limit : <number of objects to return from collection>]

因此,如果您要排序&限制映射减少输出,必须将结果存储在temp集合中.我认为无法应用排序和限制inline mapreduce输出,因为它在RAM上运行.

So if you want to sort & limit the map reduce output, you must store the result in temp collection. I dont think its possible to apply the sort & limit to the inline mapreduce output since it runs on RAM.

存在一个未解决的问题(添加了对排序和限制内联地图/缩小的支持 )在mongodb Jira中对此进行了说明.看看.

There is a open issue (Add support for sorting and limiting inline map/reduce) in mongodb Jira regarding this. Have a look.

这篇关于在MongoDB中限制和排序内联Map/Reduce的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆