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 these I need the max user_id, so I sort them in descending order and limit to 1.
But this second sort forgets the limit of 2000 and sorts over over the entire cursor from find().
Any work-around?
$cursor = $collection ->find($query) // too many entries
->sort(array('user_id'=>1)) // go ascending
->limit(2000) // got our limited qouta
->sort(array('user_id'=>-1)) // go descending to get max
->limit(1); // the one with max(user_id)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…