php8.5
Home/ Manual/ driver / bulkwritecommand/ MongoDB\Driver\BulkWriteCommand::replaceOne

MongoDB\Driver\BulkWriteCommand::replaceOne

PHP function Edit on GitHub ✎

(mongodb >=2.1.0)

Add a replaceOne operation

Description

MongoDB\Driver\BulkWriteCommand::replaceOne(string $namespace, array|object $filter, array|object $replacement, array|null $options = null): void

Add a replaceOne operation to the MongoDB\Driver\BulkWriteCommand. The first document matching filter in the collection identified by namespace will be replaced.

Parameters

replacement (array|object)

A replacement document.

options
OptionTypeDescriptionDefault
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.
sortarray|objectSpecify which document the operation replaces if the query matches multiple documents. The first document matched by the sort order will be replaced.
upsertboolIf filter does not match an existing document, insert a single document. The document will be created from replacement.false

Return Values

Void

Errors/Exceptions

Examples

MongoDB\Driver\BulkWriteCommand::replaceOne() example

php
<?php

$manager = new MongoDB\Driver\Manager;

$bulk = new MongoDB\Driver\BulkWriteCommand;
$bulk->replaceOne('db.coll', ['x' => 1], ['x' => 1, 'y' => 2]);

$result = $manager->executeBulkWriteCommand($bulk);

?>

See Also

  • MongoDB\Driver\BulkWriteCommand::updateOne
  • MongoDB\Driver\BulkWriteCommand::updateMany
  • MongoDB\Driver\Manager::executeBulkWriteCommand
  • MongoDB\Driver\BulkWriteCommandResult

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