Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 564c377

Browse files
authored
PHPC-2518: Support sort option for updateOne and replaceOne (#1794)
updateMany does not support sort. That validation is handled by mongoc_bulk_operation_update_many_with_opts().
1 parent 4726646 commit 564c377

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

‎src/MongoDB/BulkWrite.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ static bool php_phongo_bulkwrite_update_apply_options(bson_t* boptions, zval* zo
280280
PHONGO_BULKWRITE_APPEND_BOOL("upsert", upsert);
281281
PHONGO_BULKWRITE_OPT_ARRAY("arrayFilters");
282282
PHONGO_BULKWRITE_OPT_DOCUMENT("collation");
283+
PHONGO_BULKWRITE_OPT_DOCUMENT("sort");
283284

284285
if (!php_phongo_bulkwrite_opt_hint(boptions, zoptions)) {
285286
return false;

‎tests/bulk/bulkwrite-update-008.phpt‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
--TEST--
2+
MongoDB\Driver\BulkWrite::update() with sort option
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_not_live(); ?>
6+
<?php skip_if_server_version('<', '8.0'); ?>
7+
<?php skip_if_not_clean(); ?>
8+
--FILE--
9+
<?php
10+
require_once __DIR__ . "/../utils/basic.inc";
11+
12+
class CommandLogger implements MongoDB\Driver\Monitoring\CommandSubscriber
13+
{
14+
public function commandStarted(MongoDB\Driver\Monitoring\CommandStartedEvent $event): void
15+
{
16+
if ($event->getCommandName() !== 'update') {
17+
return;
18+
}
19+
20+
printf("update included sort: %s\n", json_encode($event->getCommand()->updates[0]->sort));
21+
}
22+
23+
public function commandSucceeded(MongoDB\Driver\Monitoring\CommandSucceededEvent $event): void
24+
{
25+
}
26+
27+
public function commandFailed(MongoDB\Driver\Monitoring\CommandFailedEvent $event): void
28+
{
29+
}
30+
}
31+
32+
$manager = create_test_manager();
33+
34+
$bulk = new MongoDB\Driver\BulkWrite();
35+
$bulk->insert(['_id' => 1]);
36+
$bulk->insert(['_id' => 2]);
37+
$bulk->insert(['_id' => 3]);
38+
$manager->executeBulkWrite(NS, $bulk);
39+
40+
MongoDB\Driver\Monitoring\addSubscriber(new CommandLogger);
41+
42+
$bulk = new MongoDB\Driver\BulkWrite;
43+
$bulk->update(['_id' => ['$gt' => 1]], ['$set' => ['x' => 11]], ['sort' => ['_id' => 1]]);
44+
$manager->executeBulkWrite(NS, $bulk);
45+
46+
$bulk = new MongoDB\Driver\BulkWrite;
47+
$bulk->update(['_id' => ['$gt' => 1]], ['x' => 22], ['sort' => ['_id' => -1]]);
48+
$manager->executeBulkWrite(NS, $bulk);
49+
50+
$cursor = $manager->executeQuery(NS, new MongoDB\Driver\Query([]));
51+
52+
var_dump($cursor->toArray());
53+
54+
?>
55+
===DONE===
56+
<?php exit(0); ?>
57+
--EXPECTF--
58+
update included sort: {"_id":1}
59+
update included sort: {"_id":-1}
60+
array(3) {
61+
[0]=>
62+
object(stdClass)#%d (%d) {
63+
["_id"]=>
64+
int(1)
65+
}
66+
[1]=>
67+
object(stdClass)#%d (%d) {
68+
["_id"]=>
69+
int(2)
70+
["x"]=>
71+
int(11)
72+
}
73+
[2]=>
74+
object(stdClass)#%d (%d) {
75+
["_id"]=>
76+
int(3)
77+
["x"]=>
78+
int(22)
79+
}
80+
}
81+
===DONE===
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
MongoDB\Driver\BulkWrite::update() with multi:true prohibits sort option
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$manager = create_test_manager();
10+
$bulk = new MongoDB\Driver\BulkWrite;
11+
12+
echo throws(function() use ($manager, $bulk) {
13+
$bulk->update(['x' => ['$gt' => 1]], ['$set' => ['y' => 11]], ['multi' => true, 'sort' => ['x' => 1]]);
14+
}, MongoDB\Driver\Exception\InvalidArgumentException::class), "\n";
15+
16+
?>
17+
===DONE===
18+
<?php exit(0); ?>
19+
--EXPECT--
20+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
21+
Invalid option 'sort'
22+
===DONE===

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /