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 +18
-1
lines changed Expand file tree Collapse file tree 2 files changed +18
-1
lines changed Original file line number Diff line number Diff line change @@ -273,7 +273,11 @@ public function toMql(): array
273273 $ aggregations = blank ($ this ->aggregate ['columns ' ]) ? [] : $ this ->aggregate ['columns ' ];
274274
275275 if (in_array ('* ' , $ aggregations ) && $ function == 'count ' ) {
276- return ['estimatedDocumentCount ' => [$ wheres , []]];
276+ if ($ wheres ) {
277+ return ['countDocuments ' => [$ wheres , []]];
278+ }
279+
280+ return ['estimatedDocumentCount ' => [[], []]];
277281 } elseif ($ function == 'count ' ) {
278282 // Translate count into sum.
279283 $ group ['aggregate ' ] = ['$sum ' => 1 ];
Original file line number Diff line number Diff line change @@ -291,6 +291,19 @@ public function testDecrementWithRollBack(): void
291291 $ this ->assertTrue (DB ::collection ('users ' )->where ('name ' , 'klinson ' )->where ('age ' , 20 )->exists ());
292292 }
293293
294+ public function testCount ()
295+ {
296+ DB ::beginTransaction ();
297+ $ this ->assertEquals (0 , DB ::collection ('users ' )->count ());
298+ $ this ->assertEquals (0 , DB ::collection ('users ' )->where ('age ' , 20 )->count ());
299+ DB ::collection ('users ' )->insert (['name ' => 'klinson ' , 'age ' => 20 , 'title ' => 'admin ' ]);
300+ DB ::collection ('users ' )->insert (['name ' => 'bryan ' , 'age ' => 18 , 'title ' => 'user ' ]);
301+ $ this ->assertEquals (2 , DB ::collection ('users ' )->count ());
302+ $ this ->assertEquals (1 , DB ::collection ('users ' )->where ('age ' , 20 )->count ());
303+ $ this ->assertEquals (0 , DB ::collection ('users ' )->where ('age ' , 10 )->count ());
304+ DB ::rollBack ();
305+ }
306+
294307 public function testQuery ()
295308 {
296309 /** rollback test */
You can’t perform that action at this time.
0 commit comments