Skip to content

PHPLIB-810: Always use count command for estimatedDocumentCount #926

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 2 additions & 43 deletions src/Operation/EstimatedDocumentCount.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

namespace MongoDB\Operation;

use MongoDB\Driver\Exception\CommandException;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference;
Expand All @@ -29,7 +28,6 @@

use function array_intersect_key;
use function is_integer;
use function MongoDB\server_supports_feature;

/**
* Operation for obtaining an estimated count of documents in a collection
Expand Down Expand Up @@ -111,25 +109,7 @@ public function __construct($databaseName, $collectionName, array $options = [])
*/
public function execute(Server $server)
{
$command = $this->createCommand($server);

if ($command instanceof Aggregate) {
try {
$cursor = $command->execute($server);
} catch (CommandException $e) {
if ($e->getCode() == self::$errorCodeCollectionNotFound) {
return 0;
}

throw $e;
}

$cursor->rewind();

return $cursor->current()->n;
}

return $command->execute($server);
return $this->createCount()->execute($server);
}

/**
Expand All @@ -141,28 +121,7 @@ public function execute(Server $server)
*/
public function getCommandDocument(Server $server)
{
return $this->createCommand($server)->getCommandDocument($server);
}

private function createAggregate(): Aggregate
{
return new Aggregate(
$this->databaseName,
$this->collectionName,
[
['$collStats' => ['count' => (object) []]],
['$group' => ['_id' => 1, 'n' => ['$sum' => '$count']]],
],
$this->options
);
}

/** @return Aggregate|Count */
private function createCommand(Server $server)
{
return server_supports_feature($server, self::$wireVersionForCollStats)
? $this->createAggregate()
: $this->createCount();
return $this->createCount()->getCommandDocument($server);
}

private function createCount(): Count
Expand Down
19 changes: 2 additions & 17 deletions tests/SpecTests/atlas_data_lake/estimatedDocumentCount.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,9 @@
{
"command_started_event": {
"command": {
"aggregate": "driverdata",
"pipeline": [
{
"$collStats": {
"count": {}
}
},
{
"$group": {
"_id": 1,
"n": {
"$sum": "$count"
}
}
}
]
"count": "driverdata"
},
"command_name": "aggregate",
"command_name": "count",
"database_name": "test"
}
}
Expand Down
246 changes: 0 additions & 246 deletions tests/SpecTests/retryable-reads/estimatedDocumentCount-4.9.json

This file was deleted.

Loading