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
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
),
));