File tree Expand file tree Collapse file tree 1 file changed +23
-4
lines changed
src/Jenssegers/Mongodb/Query Expand file tree Collapse file tree 1 file changed +23
-4
lines changed Original file line number Diff line number Diff line change @@ -270,11 +270,30 @@ public function getFresh($columns = [])
270
270
$ column = implode ('. ' , $ splitColumns );
271
271
}
272
272
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.
275
294
$ group ['aggregate ' ] = ['$sum ' => 1 ];
276
- } // Pass other functions directly.
277
- else {
295
+
296
+ } else {
278
297
$ group ['aggregate ' ] = ['$ ' . $ function => '$ ' . $ column ];
279
298
}
280
299
}
You can’t perform that action at this time.
0 commit comments