Skip to content

Commit ce786e6

Browse files
committed
count
1 parent 3166fc5 commit ce786e6

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/Jenssegers/Mongodb/Query/Builder.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,30 @@ public function getFresh($columns = [])
270270
$column = implode('.', $splitColumns);
271271
}
272272

273-
// Translate count into sum.
274-
if ($function == 'count') {
273+
// Null coalense only > 7.2
274+
275+
$aggregations = blank($this->aggregate['collumns']) ? [] : $this->aggregate['collumns'];
276+
277+
if (in_array('*', $aggregations) && $function == 'count') {
278+
// When ORM is paginating, count doesnt need a aggregation, just a cursor operation
279+
// elseif added to use this only in pagination
280+
// https://docs.mongodb.com/manual/reference/method/cursor.count/
281+
// count method returns int
282+
283+
$totalResults = $this->collection->count($wheres);
284+
// Preserving format expected by framework
285+
$results = [
286+
[
287+
"_id" => null,
288+
"aggregate" => $totalResults
289+
]
290+
];
291+
return $this->useCollections ? new Collection($results) : $results;
292+
} elseif ($function == 'count') {
293+
// Translate count into sum.
275294
$group['aggregate'] = ['$sum' => 1];
276-
} // Pass other functions directly.
277-
else {
295+
296+
} else {
278297
$group['aggregate'] = ['$' . $function => '$' . $column];
279298
}
280299
}

0 commit comments

Comments
 (0)