Skip to content

Commit 2a6e5d1

Browse files
committed
PHP-1320: $batch->update() fails when replacing entire document
Doesn't seem to be much point in providing a special replaceOne method. Thats something userland can do ontop of $batch->update()
1 parent 41f8b82 commit 2a6e5d1

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/MongoDB/WriteBatch.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,22 @@ PHP_METHOD(WriteBatch, update)
156156
}
157157

158158
if (limit) {
159-
mongoc_bulk_operation_update_one(intern->batch, bquery, bupdate, upsert);
159+
bson_iter_t iter;
160+
zend_bool replaced = 0;
161+
162+
if (bson_iter_init(&iter, bupdate)) {
163+
while (bson_iter_next (&iter)) {
164+
if (!strchr (bson_iter_key (&iter), '$')) {
165+
mongoc_bulk_operation_replace_one(intern->batch, bquery, bupdate, upsert);
166+
replaced = 1;
167+
break;
168+
}
169+
}
170+
}
171+
172+
if (!replaced) {
173+
mongoc_bulk_operation_update_one(intern->batch, bquery, bupdate, upsert);
174+
}
160175
} else {
161176
mongoc_bulk_operation_update(intern->batch, bquery, bupdate, upsert);
162177
}

0 commit comments

Comments
 (0)