Skip to content

Commit dcd6e84

Browse files
authored
PHPLIB-293 Add support for a comment parameter to the aggregate command (#432)
* PHPLIB-293 Add support for a comment parameter to the aggregate command
1 parent 7508380 commit dcd6e84

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

docs/includes/apiargs-MongoDBCollection-method-aggregate-option.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ post: |
3535
version of MongoDB, this option will be ignored.
3636
---
3737
arg_name: option
38+
name: comment
39+
type: string
40+
description: |
41+
Users can specify an arbitrary string to help trace the operation through the
42+
database profiler, currentOp, and logs.
43+
44+
.. versionadded:: 1.3
45+
interface: phpmethod
46+
operation: ~
47+
optional: true
48+
---
49+
arg_name: option
3850
name: hint
3951
type: string|array|object
4052
description: |

src/Operation/Aggregate.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ class Aggregate implements Executable
7474
* This is not supported for server versions < 3.4 and will result in an
7575
* exception at execution time if used.
7676
*
77+
* * comment (string): An arbitrary string to help trace the operation
78+
* through the database profiler, currentOp, and logs.
79+
*
7780
* * hint (string|document): The index to use. Specify either the index
7881
* name as a string or the index key pattern as a document. If specified,
7982
* then the query system will only consider plans using the hinted index.
@@ -150,6 +153,10 @@ public function __construct($databaseName, $collectionName, array $pipeline, arr
150153
throw InvalidArgumentException::invalidType('"collation" option', $options['collation'], 'array or object');
151154
}
152155

156+
if (isset($options['comment']) && ! is_string($options['comment'])) {
157+
throw InvalidArgumentException::invalidType('"comment" option', $options['comment'], 'string');
158+
}
159+
153160
if (isset($options['hint']) && ! is_string($options['hint']) && ! is_array($options['hint']) && ! is_object($options['hint'])) {
154161
throw InvalidArgumentException::invalidType('"hint" option', $options['hint'], 'string or array or object');
155162
}
@@ -276,6 +283,12 @@ private function createCommand(Server $server, $isCursorSupported)
276283
$cmd['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
277284
}
278285

286+
foreach (['comment', 'maxTimeMS'] as $option) {
287+
if (isset($this->options[$option])) {
288+
$cmd[$option] = $this->options[$option];
289+
}
290+
}
291+
279292
if (isset($this->options['collation'])) {
280293
$cmd['collation'] = (object) $this->options['collation'];
281294
}
@@ -284,10 +297,6 @@ private function createCommand(Server $server, $isCursorSupported)
284297
$cmd['hint'] = is_array($this->options['hint']) ? (object) $this->options['hint'] : $this->options['hint'];
285298
}
286299

287-
if (isset($this->options['maxTimeMS'])) {
288-
$cmd['maxTimeMS'] = $this->options['maxTimeMS'];
289-
}
290-
291300
if (isset($this->options['readConcern'])) {
292301
$cmd['readConcern'] = \MongoDB\read_concern_as_document($this->options['readConcern']);
293302
}

tests/Operation/AggregateTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public function provideInvalidConstructorOptions()
4444
$options[][] = ['collation' => $value];
4545
}
4646

47+
foreach ($this->getInvalidStringValues() as $value) {
48+
$options[][] = ['comment' => $value];
49+
}
50+
4751
foreach ($this->getInvalidHintValues() as $value) {
4852
$options[][] = ['hint' => $value];
4953
}

0 commit comments

Comments
 (0)