This repository was archived by the owner on Aug 22, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +30
-1
lines changed Expand file tree Collapse file tree 2 files changed +30
-1
lines changed Original file line number Diff line number Diff line change @@ -273,7 +273,11 @@ public function toMql(): array
273
273
$ aggregations = blank ($ this ->aggregate ['columns ' ]) ? [] : $ this ->aggregate ['columns ' ];
274
274
275
275
if (in_array ('* ' , $ aggregations ) && $ function == 'count ' ) {
276
- return ['estimatedDocumentCount ' => [$ wheres , []]];
276
+ if ($ wheres ) {
277
+ return ['countDocuments ' => [$ wheres , []]];
278
+ }
279
+
280
+ return ['estimatedDocumentCount ' => [[], []]];
277
281
} elseif ($ function == 'count ' ) {
278
282
// Translate count into sum.
279
283
$ group ['aggregate ' ] = ['$sum ' => 1 ];
Original file line number Diff line number Diff line change @@ -291,6 +291,31 @@ public function testDecrementWithRollBack(): void
291
291
$ this ->assertTrue (DB ::collection ('users ' )->where ('name ' , 'klinson ' )->where ('age ' , 20 )->exists ());
292
292
}
293
293
294
+ /**
295
+ * @testWith [false]
296
+ * [true]
297
+ */
298
+ public function testCount (bool $ transaction )
299
+ {
300
+ if ($ transaction ) {
301
+ DB ::beginTransaction ();
302
+ }
303
+
304
+ $ this ->assertEquals (0 , DB ::collection ('users ' )->count ());
305
+ $ this ->assertEquals (0 , DB ::collection ('users ' )->where ('age ' , 20 )->count ());
306
+ DB ::collection ('users ' )->insert (['name ' => 'klinson ' , 'age ' => 20 , 'title ' => 'admin ' ]);
307
+ DB ::collection ('users ' )->insert (['name ' => 'bryan ' , 'age ' => 18 , 'title ' => 'user ' ]);
308
+ $ this ->assertEquals (2 , DB ::collection ('users ' )->count ());
309
+
310
+ if ($ transaction ) {
311
+ DB ::commit ();
312
+ }
313
+
314
+ // filtered count result is incorrect until transaction is committed
315
+ $ this ->assertEquals (1 , DB ::collection ('users ' )->where ('age ' , 20 )->count ());
316
+ $ this ->assertEquals (0 , DB ::collection ('users ' )->where ('age ' , 10 )->count ());
317
+ }
318
+
294
319
public function testQuery ()
295
320
{
296
321
/** rollback test */
You can’t perform that action at this time.
0 commit comments