mongo sort after limit after sort - Not working

后端 未结 5 1842
余生分开走
余生分开走 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:51

    Answer by Sammaye is correct.

    You can still achieve the way you wanted. You can use aggregation framework, as it executes one stage after other stage and so on.

    $doc = $collection->aggregate(array(
        array(
          '$match' => $query,
        ),
        array(
          '$sort' => array(
              'user_id'=> 1
          ),
        ),
        array(
          '$limit' => 2000
        ),
        array(
          '$sort' => array(
              'user_id'=> -1
          ),
        ),
        array(
          '$limit' => 1
        ),
    ));
    

提交回复
热议问题