Skip to content

Commit 2f502c2

Browse files
committed
Merge pull request #1008
2 parents 5366a41 + 3bad73d commit 2f502c2

File tree

2 files changed

+110
-1
lines changed

2 files changed

+110
-1
lines changed

src/MongoDB/BulkWrite.c

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,50 @@ static inline bool php_phongo_bulkwrite_update_has_operators(bson_t* bupdate) /*
7373
return false;
7474
} /* }}} */
7575

76+
/* Returns whether the update document is considered an aggregation pipeline */
77+
static inline bool php_phongo_bulkwrite_update_is_pipeline(bson_t* bupdate) /* {{{ */
78+
{
79+
bson_iter_t iter;
80+
bson_iter_t child;
81+
const char* key;
82+
int i = 0;
83+
char* i_str;
84+
85+
if (!bson_iter_init(&iter, bupdate)) {
86+
return false;
87+
}
88+
89+
while (bson_iter_next(&iter)) {
90+
key = bson_iter_key(&iter);
91+
i_str = bson_strdup_printf("%d", i++);
92+
93+
if (strcmp(key, i_str)) {
94+
bson_free(i_str);
95+
return false;
96+
}
97+
98+
bson_free(i_str);
99+
100+
if (BSON_ITER_HOLDS_DOCUMENT(&iter)) {
101+
if (!bson_iter_recurse(&iter, &child)) {
102+
return false;
103+
}
104+
if (!bson_iter_next(&child)) {
105+
return false;
106+
}
107+
key = bson_iter_key(&child);
108+
if (key[0] != '$') {
109+
return false;
110+
}
111+
} else {
112+
return false;
113+
}
114+
}
115+
116+
/* should return false when the document is empty */
117+
return i != 0;
118+
} /* }}} */
119+
76120
/* Returns whether the BSON array's keys are a sequence of integer strings
77121
* starting with "0". BSON_APPEND_ARRAY considers it the caller's responsibility
78122
* to ensure that the array's keys are properly formatted. */
@@ -345,7 +389,7 @@ static PHP_METHOD(BulkWrite, update)
345389
goto cleanup;
346390
}
347391

348-
if (php_phongo_bulkwrite_update_has_operators(&bupdate)) {
392+
if (php_phongo_bulkwrite_update_has_operators(&bupdate) || php_phongo_bulkwrite_update_is_pipeline(&bupdate)) {
349393
if (zoptions && php_array_existsc(zoptions, "multi") && php_array_fetchc_bool(zoptions, "multi")) {
350394
if (!mongoc_bulk_operation_update_many_with_opts(intern->bulk, &bquery, &bupdate, &boptions, &error)) {
351395
phongo_throw_exception_from_bson_error_t(&error TSRMLS_CC);

tests/bulk/bulkwrite-update-003.phpt

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
--TEST--
2+
MongoDB\Driver\BulkWrite::update() with pipeline option
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_not_live(); ?>
6+
<?php skip_if_server_version('<', '4.2'); ?>
7+
<?php skip_if_not_clean(); ?>
8+
--FILE--
9+
<?php
10+
require_once __DIR__ . "/../utils/basic.inc";
11+
12+
$manager = new MongoDB\Driver\Manager(URI);
13+
14+
$bulk = new MongoDB\Driver\BulkWrite();
15+
16+
$bulk->insert([ '_id' => 1, 'x' => 1, 'y' => 1, 't' => [ 'u' => [ 'v' => 1 ] ] ]);
17+
$bulk->insert([ '_id' => 2, 'x' => 2, 'y' => 1]);
18+
19+
$manager->executeBulkWrite(NS, $bulk);
20+
21+
$updateBulk = new MongoDB\Driver\BulkWrite();
22+
23+
$query = ['_id' => 1];
24+
$update = [
25+
[
26+
'$replaceRoot' => [ 'newRoot' => '$t' ],
27+
],
28+
[
29+
'$addFields' => [ 'foo' => 1 ],
30+
],
31+
];
32+
33+
$updateBulk->update($query, $update);
34+
$manager->executeBulkWrite(NS, $updateBulk);
35+
36+
$cursor = $manager->executeQuery(NS, new \MongoDB\Driver\Query([]));
37+
var_dump($cursor->toArray());
38+
?>
39+
===DONE===
40+
<?php exit(0); ?>
41+
--EXPECTF--
42+
array(%d) {
43+
[0]=>
44+
object(stdClass)#%d (%d) {
45+
["_id"]=>
46+
int(1)
47+
["u"]=>
48+
object(stdClass)#%d (%d) {
49+
["v"]=>
50+
int(1)
51+
}
52+
["foo"]=>
53+
int(1)
54+
}
55+
[1]=>
56+
object(stdClass)#%d (%d) {
57+
["_id"]=>
58+
int(2)
59+
["x"]=>
60+
int(2)
61+
["y"]=>
62+
int(1)
63+
}
64+
}
65+
===DONE===

0 commit comments

Comments
 (0)