I decided to do this:
$userPosition = StatsModel::where(...)->...->limit(50)->get('_id');
$count = count($userPosition)
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I need to get the position of the user. To do this, I count the number of records where the value is greater than mine. In case the values are equal, then I count the values where the time is less than my time. If the values are equal and the time is equal, then I look at the date. I am making a request like this:
$userPosition = StatsModel::where('user_id', '!=', Auth::id())
->where(function ($query) use ($currentUserValue, $currentUserTime, $currentUserDate) {
$query->where('stats.value', '>', $currentUserValue)
->orWhere(function ($query) use ($currentUserValue, $currentUserTime, $currentUserDate) {
$query->where('stats.value', '=', $currentUserValue)
->where('stats,.time', '<', $currentUserTime);
})
->orWhere(function ($query) use($currentUserValue, $currentUserTime, $currentUserDate) {
$query->where('stats.value', '=', $currentUserValue)
->where('stats.time', '=', $currentUserTime)
->where('stats.date', '<', $currentUserDate);
});
})
->count();
I'm interested in the following: how to make a limit? I don't want to count all the records in the database, it's enough for me to reach, for example, up to 50 and stop counting. I tried:
->limit(50)->count();
->take(50)->count();
These methods do not help. I am using mongodb (jenssegers/mongodb)
Please or to participate in this conversation.