Skip to content

Commit d620cc0

Browse files
PHPLIB-810: Always use count command for estimatedDocumentCount (#926)
Spec tests synced with mongodb/specifications@021cbc8 Co-authored-by: Jeremy Mikola <[email protected]>
1 parent 67eebe5 commit d620cc0

20 files changed

+501
-1542
lines changed

src/Operation/EstimatedDocumentCount.php

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
namespace MongoDB\Operation;
1919

20-
use MongoDB\Driver\Exception\CommandException;
2120
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
2221
use MongoDB\Driver\ReadConcern;
2322
use MongoDB\Driver\ReadPreference;
@@ -29,7 +28,6 @@
2928

3029
use function array_intersect_key;
3130
use function is_integer;
32-
use function MongoDB\server_supports_feature;
3331

3432
/**
3533
* Operation for obtaining an estimated count of documents in a collection
@@ -111,25 +109,7 @@ public function __construct($databaseName, $collectionName, array $options = [])
111109
*/
112110
public function execute(Server $server)
113111
{
114-
$command = $this->createCommand($server);
115-
116-
if ($command instanceof Aggregate) {
117-
try {
118-
$cursor = $command->execute($server);
119-
} catch (CommandException $e) {
120-
if ($e->getCode() == self::$errorCodeCollectionNotFound) {
121-
return 0;
122-
}
123-
124-
throw $e;
125-
}
126-
127-
$cursor->rewind();
128-
129-
return $cursor->current()->n;
130-
}
131-
132-
return $command->execute($server);
112+
return $this->createCount()->execute($server);
133113
}
134114

135115
/**
@@ -141,28 +121,7 @@ public function execute(Server $server)
141121
*/
142122
public function getCommandDocument(Server $server)
143123
{
144-
return $this->createCommand($server)->getCommandDocument($server);
145-
}
146-
147-
private function createAggregate(): Aggregate
148-
{
149-
return new Aggregate(
150-
$this->databaseName,
151-
$this->collectionName,
152-
[
153-
['$collStats' => ['count' => (object) []]],
154-
['$group' => ['_id' => 1, 'n' => ['$sum' => '$count']]],
155-
],
156-
$this->options
157-
);
158-
}
159-
160-
/** @return Aggregate|Count */
161-
private function createCommand(Server $server)
162-
{
163-
return server_supports_feature($server, self::$wireVersionForCollStats)
164-
? $this->createAggregate()
165-
: $this->createCount();
124+
return $this->createCount()->getCommandDocument($server);
166125
}
167126

168127
private function createCount(): Count

tests/SpecTests/atlas_data_lake/estimatedDocumentCount.json

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,9 @@
1515
{
1616
"command_started_event": {
1717
"command": {
18-
"aggregate": "driverdata",
19-
"pipeline": [
20-
{
21-
"$collStats": {
22-
"count": {}
23-
}
24-
},
25-
{
26-
"$group": {
27-
"_id": 1,
28-
"n": {
29-
"$sum": "$count"
30-
}
31-
}
32-
}
33-
]
18+
"count": "driverdata"
3419
},
35-
"command_name": "aggregate",
20+
"command_name": "count",
3621
"database_name": "test"
3722
}
3823
}

tests/SpecTests/retryable-reads/estimatedDocumentCount-4.9.json

Lines changed: 0 additions & 246 deletions
This file was deleted.

0 commit comments

Comments
 (0)