Enhance count method in Client class and expand test coverage

This commit updates the count method in the Client class to include a call to cleanFilters for improved filter handling. Additionally, it adds new test cases in MongoTest.php to validate counting with the $or operator and complex conditions, ensuring robust testing of the counting functionality.
This commit is contained in:
shimon committed 2025-09-11 15:48:48 +03:00
1 parent f2b48fae32
commit 7189b96598
2 files changed
+23 -1

No files matched your search

+4 -1
View File
@@ -707,12 +707,15 @@ class Client
*/
public function count(string $collection, array $filters, array $options): int
{
$filters = $this->cleanFilters($filters);
// Use MongoDB's native count command with the working format instad of running find and count the results
$command = [
self::COMMAND_COUNT => $collection,
'query' => $this->toObject($filters),
];
var_dump($command);
// Add limit if specified
if (isset($options['limit'])) {
$command['limit'] = (int)$options['limit'];
+19
View File
@@ -298,6 +298,11 @@ class MongoTest extends TestCase
$total = $this->getDatabase()->count($collectionName, ['number' => ['$lte' => 10]], ['limit' => 3]);
self::assertEquals(3, $total);
// Test count with $or operator and comparison (should be 2 documents with number <= 2 OR number >= 29)
$total = $this->getDatabase()->count($collectionName, ['$or' => [['number' => ['$lte' => 2]], ['number' => ['$gte' => 29]]]], []);
self::assertEquals(4, $total);
// Test aggregation count - total documents
$aggregationResult = $this->getDatabase()->aggregate($collectionName, [
['$count' => 'total']
@@ -328,6 +333,20 @@ class MongoTest extends TestCase
self::assertEquals(30, $groupedAggregationResult->cursor->firstBatch[0]->count);
self::assertEquals('test', $groupedAggregationResult->cursor->firstBatch[0]->_id);
// Test aggregation count with $or operator
$orAggregationResult = $this->getDatabase()->aggregate($collectionName, [
['$match' => ['$or' => [['number' => 5], ['number' => 15], ['number' => 25]]]],
['$count' => 'total']
]);
self::assertEquals(3, $orAggregationResult->cursor->firstBatch[0]->total);
// Test aggregation count with complex $or and range
$complexOrAggregationResult = $this->getDatabase()->aggregate($collectionName, [
['$match' => ['$or' => [['number' => ['$lte' => 3]], ['number' => ['$gte' => 28]]]]],
['$count' => 'total']
]);
self::assertEquals(6, $complexOrAggregationResult->cursor->firstBatch[0]->total);
$this->getDatabase()->dropCollection($collectionName);
}