Skip to content

Commit 9960219

Browse files
committed
Aggregate option explain is incompatible with the explain command
The 'explain' option is illegal when a explain verbosity is also provided
1 parent 1f7abd5 commit 9960219

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

src/Operation/Aggregate.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,10 @@ public function getCommandDocument()
300300
{
301301
$cmd = $this->createCommandDocument();
302302

303+
// The 'explain' option is incompatible with the explain command
304+
unset($cmd['explain']);
305+
unset($cmd['cursor']);
306+
303307
// Read concern can change the query plan
304308
if (isset($this->options['readConcern'])) {
305309
$cmd['readConcern'] = $this->options['readConcern'];

tests/Operation/AggregateTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace MongoDB\Tests\Operation;
44

5+
use MongoDB\Driver\ReadConcern;
6+
use MongoDB\Driver\ReadPreference;
7+
use MongoDB\Driver\WriteConcern;
58
use MongoDB\Exception\InvalidArgumentException;
69
use MongoDB\Operation\Aggregate;
710

@@ -104,4 +107,40 @@ private function getInvalidHintValues()
104107
{
105108
return [123, 3.14, true];
106109
}
110+
111+
public function testExplainableCommandDocument(): void
112+
{
113+
$options = [
114+
'allowDiskUse' => true,
115+
'batchSize' => 100,
116+
'bypassDocumentValidation' => true,
117+
'collation' => ['locale' => 'fr'],
118+
'comment' => 'explain me',
119+
'hint' => '_id_',
120+
'let' => ['a' => 1],
121+
'maxTimeMS' => 100,
122+
'readConcern' => new ReadConcern(ReadConcern::LOCAL),
123+
'useCursor' => true,
124+
// Intentionally omitted options
125+
'explain' => true,
126+
'readPreference' => new ReadPreference(ReadPreference::SECONDARY_PREFERRED),
127+
'typeMap' => ['root' => 'array', 'document' => 'array'],
128+
'writeConcern' => new WriteConcern(0),
129+
];
130+
$operation = new Aggregate($this->getDatabaseName(), $this->getCollectionName(), [['$project' => ['_id' => 0]]], $options);
131+
132+
$expected = [
133+
'aggregate' => $this->getCollectionName(),
134+
'pipeline' => [['$project' => ['_id' => 0]]],
135+
'allowDiskUse' => true,
136+
'bypassDocumentValidation' => true,
137+
'collation' => (object) ['locale' => 'fr'],
138+
'comment' => 'explain me',
139+
'hint' => '_id_',
140+
'maxTimeMS' => 100,
141+
'readConcern' => new ReadConcern(ReadConcern::LOCAL),
142+
'let' => (object) ['a' => 1],
143+
];
144+
$this->assertEquals($expected, $operation->getCommandDocument());
145+
}
107146
}

tests/Operation/DistinctTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use MongoDB\Driver\ReadConcern;
66
use MongoDB\Driver\ReadPreference;
7-
use MongoDB\Driver\WriteConcern;
87
use MongoDB\Exception\InvalidArgumentException;
98
use MongoDB\Operation\Distinct;
109

0 commit comments

Comments
 (0)