Limit and sort inline Map/Reduce in MongoDB

前端 未结 1 684
天涯浪人
天涯浪人 2021-01-28 21:11

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. Thi

相关标签:
1条回答
  • 2021-01-28 21:14

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

    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>]
    

    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.

    EDIT:

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

    0 讨论(0)
提交回复
热议问题