我有一个集合,我从中获得使用$ query的特定类型的用户
然后我需要根据user_id升序对它们进行排序并将它们限制为2000
从这些我需要max user_id,所以我按降序排序并限制为1.
但是第二种类型会忘记2000的限制,并从find()中对整个游标进行排序.
任何解决方法?
$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)
Run Code Online (Sandbox Code Playgroud)