Skip to content
This repository was archived by the owner on Aug 22, 2023. It is now read-only.

Commit 1905070

Browse files
committed
Use countDocument or estimatedDocumentsCount when no filter is provided
Add functional test
1 parent db771b1 commit 1905070

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ All notable changes to this project will be documented in this file.
1010
- Throw an exception for unsupported `Query\Builder` methods [#9](https://github.com/GromNaN/laravel-mongodb-private/pull/9) by [@GromNaN](https://github.com/GromNaN).
1111
- Throw an exception when `Query\Builder::orderBy()` is used with invalid direction [#7](https://github.com/GromNaN/laravel-mongodb-private/pull/7) by [@GromNaN](https://github.com/GromNaN).
1212
- Throw an exception when `Query\Builder::push()` is used incorrectly [#5](https://github.com/GromNaN/laravel-mongodb-private/pull/5) by [@GromNaN](https://github.com/GromNaN).
13-
- Remove call to deprecated `Collection::count` for `countDocuments` [#18](https://github.com/GromNaN/laravel-mongodb-private/pull/18) by [@GromNaN](https://github.com/GromNaN).
13+
- Remove call to deprecated `Collection::count` for `countDocuments` and `estimatedDocumentCount` [#18](https://github.com/GromNaN/laravel-mongodb-private/pull/18) by [@GromNaN](https://github.com/GromNaN).
1414

1515
## [3.9.2] - 2022-09-01
1616

src/Query/Builder.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff 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];

tests/TransactionTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,35 @@ public function testDecrementWithRollBack(): void
291291
$this->assertTrue(DB::collection('users')->where('name', 'klinson')->where('age', 20)->exists());
292292
}
293293

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+
// Counting fetched results
311+
$this->assertEquals(1, DB::collection('users')->where('age', 20)->get()->count());
312+
313+
if ($transaction) {
314+
// until transaction is committed, count with filter is incorrect
315+
$this->assertEquals(0, DB::collection('users')->where('age', 20)->count());
316+
DB::commit();
317+
}
318+
319+
$this->assertEquals(1, DB::collection('users')->where('age', 20)->count());
320+
$this->assertEquals(0, DB::collection('users')->where('age', 10)->count());
321+
}
322+
294323
public function testQuery()
295324
{
296325
/** rollback test */

0 commit comments

Comments
 (0)