php8.5
Home/ Manual/ driver / bulkwrite/ MongoDB\Driver\BulkWrite::update

MongoDB\Driver\BulkWrite::update

PHP function Edit on GitHub ✎

(mongodb >=1.0.0)

Add an update operation to the bulk

Description

MongoDB\Driver\BulkWrite::update(array|object $filter, array|object $newObj, array|null $updateOptions = null): void

Adds an update operation to the MongoDB\Driver\BulkWrite.

Parameters

newObj (array|object)

A document containing either update operators (e.g. $set), a replacement document (i.e. only field:value expressions), or an aggregation pipeline.

updateOptions
OptionTypeDescriptionDefault
arrayFiltersarrayAn array of filter documents that determines which array elements to modify for an update operation on an array field. See Specify arrayFilters for Array Update Operations in the MongoDB manual for more information.This option is available in MongoDB 3.6+ and will result in an exception at execution time if specified for an older server version.
hintstring|array|objectIndex specification. Specify either the index name as a string or the index key pattern. If specified, then the query system will only consider plans using the hinted index.This option is available in MongoDB 4.2+ and will result in an exception at execution time if specified for an older server version.
multiboolUpdate only the first matching document if false, or all matching documents true. This option cannot be true if newObj is a replacement document.false
sortarray|objectSpecify which document the operation updates if the query matches multiple documents. The first document matched by the sort order will be updated.This option cannot be used if "multi" is true.This option is available in MongoDB 8.0+ and will result in an exception at execution time if specified for an older server version.
upsertboolIf filter does not match an existing document, insert a single document. The document will be created from newObj if it is a replacement document (i.e. no update operators); otherwise, the operators in newObj will be applied to filter to create the new document.false

Return Values

Void

Errors/Exceptions

Changelog

VersionDescription
PECL mongodb 1.21.0Added the "sort" option.
PECL mongodb 1.7.0Added the "hint" option.
PECL mongodb 1.6.0The newObj parameter now accepts an aggregation pipeline. This feature requires MongoDB 4.2+ and will result in an exception at execution time if specified for an older server version.
PECL mongodb 1.5.0Using the "arrayFilters" option will result in an exception at execution time if unsupported by the server. Previously, no exception would be thrown and the option may have been ignored.
PECL mongodb 1.4.0Added the "arrayFilters" option.
PECL mongodb 1.2.0Added the "collation" option.

Examples

MongoDB\Driver\BulkWrite::update() example

php
<?php

$bulk = new MongoDB\Driver\BulkWrite;
$bulk->update(
    ['x' => 2],
    ['$set' => ['y' => 3]],
    ['multi' => false, 'upsert' => false]
);

$manager = new MongoDB\Driver\Manager('mongodb://localhost:27017');
$result = $manager->executeBulkWrite('db.collection', $bulk);

?>

See Also

  • MongoDB\Driver\Manager::executeBulkWrite
  • MongoDB\Driver\WriteResult

Source: reference/mongodb/mongodb/driver/bulkwrite/update.xml · from the official PHP manual (php/doc-en)