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

Commit

Permalink
Use countDocument or estimatedDocumentsCount when no filter is provided
Browse files Browse the repository at this point in the history
Add functional test
  • Loading branch information
GromNaN committed Jul 20, 2023
1 parent db771b1 commit 1905070
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ All notable changes to this project will be documented in this file.
- Throw an exception for unsupported `Query\Builder` methods [#9](https://github.com/GromNaN/laravel-mongodb-private/pull/9) by [@GromNaN](https://github.com/GromNaN).
- 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).
- 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).
- Remove call to deprecated `Collection::count` for `countDocuments` [#18](https://github.com/GromNaN/laravel-mongodb-private/pull/18) by [@GromNaN](https://github.com/GromNaN).
- 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).

## [3.9.2] - 2022-09-01

Expand Down
6 changes: 5 additions & 1 deletion src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,11 @@ public function toMql(): array
$aggregations = blank($this->aggregate['columns']) ? [] : $this->aggregate['columns'];

if (in_array('*', $aggregations) && $function == 'count') {
return ['estimatedDocumentCount' => [$wheres, []]];
if ($wheres) {
return ['countDocuments' => [$wheres, []]];
}

return ['estimatedDocumentCount' => [[], []]];
} elseif ($function == 'count') {
// Translate count into sum.
$group['aggregate'] = ['$sum' => 1];
Expand Down
29 changes: 29 additions & 0 deletions tests/TransactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,35 @@ public function testDecrementWithRollBack(): void
$this->assertTrue(DB::collection('users')->where('name', 'klinson')->where('age', 20)->exists());
}

/**
* @testWith [false]
* [true]
*/
public function testCount(bool $transaction)
{
if ($transaction) {
DB::beginTransaction();
}

$this->assertEquals(0, DB::collection('users')->count());
$this->assertEquals(0, DB::collection('users')->where('age', 20)->count());
DB::collection('users')->insert(['name' => 'klinson', 'age' => 20, 'title' => 'admin']);
DB::collection('users')->insert(['name' => 'bryan', 'age' => 18, 'title' => 'user']);
$this->assertEquals(2, DB::collection('users')->count());

// Counting fetched results
$this->assertEquals(1, DB::collection('users')->where('age', 20)->get()->count());

if ($transaction) {
// until transaction is committed, count with filter is incorrect
$this->assertEquals(0, DB::collection('users')->where('age', 20)->count());
DB::commit();
}

$this->assertEquals(1, DB::collection('users')->where('age', 20)->count());
$this->assertEquals(0, DB::collection('users')->where('age', 10)->count());
}

public function testQuery()
{
/** rollback test */
Expand Down

0 comments on commit 1905070

Please sign in to comment.