Skip to content

Commit 97249da

Browse files
committed
PHPLIB-481: Add the ability to specify a pipeline to an update within bulk write
1 parent b164780 commit 97249da

File tree

4 files changed

+172
-6
lines changed

4 files changed

+172
-6
lines changed

src/Operation/BulkWrite.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use function is_object;
3434
use function key;
3535
use function MongoDB\is_first_key_operator;
36+
use function MongoDB\is_pipeline;
3637
use function MongoDB\server_supports_feature;
3738
use function sprintf;
3839

@@ -255,8 +256,8 @@ public function __construct($databaseName, $collectionName, array $operations, a
255256
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][1]', $i, $type), $args[1], 'array or object');
256257
}
257258

258-
if (! is_first_key_operator($args[1])) {
259-
throw new InvalidArgumentException(sprintf('First key in $operations[%d]["%s"][1] is not an update operator', $i, $type));
259+
if (! is_first_key_operator($args[1]) && ! is_pipeline($args[1])) {
260+
throw new InvalidArgumentException(sprintf('First key in $operations[%d]["%s"][1] is neither an update operator nor a pipeline', $i, $type));
260261
}
261262

262263
if (! isset($args[2])) {

tests/Operation/BulkWriteFunctionalTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ public function provideOpsWithMissingArguments()
263263
public function testUpdateOneRequiresUpdateOperators()
264264
{
265265
$this->expectException(InvalidArgumentException::class);
266-
$this->expectExceptionMessage('First key in $operations[0]["updateOne"][1] is not an update operator');
266+
$this->expectExceptionMessage('First key in $operations[0]["updateOne"][1] is neither an update operator nor a pipeline');
267267
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
268268
['updateOne' => [['_id' => 1], ['x' => 1]]],
269269
]);
@@ -272,7 +272,7 @@ public function testUpdateOneRequiresUpdateOperators()
272272
public function testUpdateManyRequiresUpdateOperators()
273273
{
274274
$this->expectException(InvalidArgumentException::class);
275-
$this->expectExceptionMessage('First key in $operations[0]["updateMany"][1] is not an update operator');
275+
$this->expectExceptionMessage('First key in $operations[0]["updateMany"][1] is neither an update operator nor a pipeline');
276276
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
277277
['updateMany' => [['_id' => ['$gt' => 1]], ['x' => 1]]],
278278
]);

tests/Operation/BulkWriteTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ public function testUpdateManyUpdateArgumentTypeCheck($update)
261261
public function testUpdateManyUpdateArgumentRequiresOperators()
262262
{
263263
$this->expectException(InvalidArgumentException::class);
264-
$this->expectExceptionMessage('First key in $operations[0]["updateMany"][1] is not an update operator');
264+
$this->expectExceptionMessage('First key in $operations[0]["updateMany"][1] is neither an update operator nor a pipeline');
265265
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
266266
[BulkWrite::UPDATE_MANY => [['_id' => ['$gt' => 1]], ['x' => 1]]],
267267
]);
@@ -348,7 +348,7 @@ public function testUpdateOneUpdateArgumentTypeCheck($update)
348348
public function testUpdateOneUpdateArgumentRequiresOperators()
349349
{
350350
$this->expectException(InvalidArgumentException::class);
351-
$this->expectExceptionMessage('First key in $operations[0]["updateOne"][1] is not an update operator');
351+
$this->expectExceptionMessage('First key in $operations[0]["updateOne"][1] is neither an update operator nor a pipeline');
352352
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
353353
[BulkWrite::UPDATE_ONE => [['_id' => 1], ['x' => 1]]],
354354
]);

tests/SpecTests/crud/updateWithPipelines.json

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,171 @@
238238
]
239239
}
240240
}
241+
},
242+
{
243+
"description": "UpdateOne in bulk write using pipelines",
244+
"operations": [
245+
{
246+
"name": "bulkWrite",
247+
"arguments": {
248+
"requests": [
249+
{
250+
"name": "updateOne",
251+
"arguments": {
252+
"filter": {
253+
"_id": 1
254+
},
255+
"update": [
256+
{
257+
"$replaceRoot": {
258+
"newRoot": "$t"
259+
}
260+
},
261+
{
262+
"$addFields": {
263+
"foo": 1
264+
}
265+
}
266+
]
267+
}
268+
}
269+
]
270+
},
271+
"result": {
272+
"matchedCount": 1,
273+
"modifiedCount": 1,
274+
"upsertedCount": 0
275+
}
276+
}
277+
],
278+
"expectations": [
279+
{
280+
"command_started_event": {
281+
"command": {
282+
"update": "test",
283+
"updates": [
284+
{
285+
"q": {
286+
"_id": 1
287+
},
288+
"u": [
289+
{
290+
"$replaceRoot": {
291+
"newRoot": "$t"
292+
}
293+
},
294+
{
295+
"$addFields": {
296+
"foo": 1
297+
}
298+
}
299+
]
300+
}
301+
]
302+
},
303+
"command_name": "update",
304+
"database_name": "crud-tests"
305+
}
306+
}
307+
],
308+
"outcome": {
309+
"collection": {
310+
"data": [
311+
{
312+
"_id": 1,
313+
"u": {
314+
"v": 1
315+
},
316+
"foo": 1
317+
},
318+
{
319+
"_id": 2,
320+
"x": 2,
321+
"y": 1
322+
}
323+
]
324+
}
325+
}
326+
},
327+
{
328+
"description": "UpdateMany in bulk write using pipelines",
329+
"operations": [
330+
{
331+
"name": "bulkWrite",
332+
"arguments": {
333+
"requests": [
334+
{
335+
"name": "updateMany",
336+
"arguments": {
337+
"filter": {},
338+
"update": [
339+
{
340+
"$project": {
341+
"x": 1
342+
}
343+
},
344+
{
345+
"$addFields": {
346+
"foo": 1
347+
}
348+
}
349+
]
350+
}
351+
}
352+
]
353+
},
354+
"result": {
355+
"matchedCount": 2,
356+
"modifiedCount": 2,
357+
"upsertedCount": 0
358+
}
359+
}
360+
],
361+
"expectations": [
362+
{
363+
"command_started_event": {
364+
"command": {
365+
"update": "test",
366+
"updates": [
367+
{
368+
"q": {},
369+
"u": [
370+
{
371+
"$project": {
372+
"x": 1
373+
}
374+
},
375+
{
376+
"$addFields": {
377+
"foo": 1
378+
}
379+
}
380+
],
381+
"multi": true
382+
}
383+
]
384+
},
385+
"command_name": "update",
386+
"database_name": "crud-tests"
387+
}
388+
}
389+
],
390+
"outcome": {
391+
"collection": {
392+
"data": [
393+
{
394+
"_id": 1,
395+
"x": 1,
396+
"foo": 1
397+
},
398+
{
399+
"_id": 2,
400+
"x": 2,
401+
"foo": 1
402+
}
403+
]
404+
}
405+
}
241406
}
242407
]
243408
}

0 commit comments

Comments
 (0)