mongo sort after limit after sort - Not working

后端 未结 5 1852
余生分开走
余生分开走 2020-12-18 02:15

I have a collection, from which i get particular type of users using $query
Then I need sort them according to user_id ascending and limit them to 2000

From t

5条回答
  •  清歌不尽
    2020-12-18 02:44

    What is the reason behind sort-limit-sort-limit approach?

    Can't you just do

    $cursor = $collection ->find($query)
                ->sort(array('user_id'=>-1))
                ->limit(1);
    

    EDIT: Also, only the last sort applied to the cursor has an effect. (This line is present in pymongo docs here, which makes sense.)

提交回复
热议问题