(No version information available, might only be in Git)
Collection::addOrReplaceOne — Add or replace collection document
$id
, string $doc
)Add a new document, or replace a document if it already exists.
Here are several scenarios for this method:
If neither the id or any unique key values conflict with any document in the collection, then the document is added.
If the id does not match any document but one or more unique key values conflict with a document in the collection, then an error is raised.
If id matches an existing document and no unique keys are defined for the collection, then the document is replaced.
If id matches an existing document, and either all unique keys in the replacement document match that same document or they don't conflict with any other documents in the collection, then the document is replaced.
If id matches an existing document and one or more unique keys match a different document from the collection, then an error is raised.
id
This is the filter id. If this id or any other field that has a unique index already exists in the collection, then it will update the matching document instead.
doc
This is the document to add or replace, which is a JSON string.
A Result object.
Example #1 mysql_xdevapi\Collection::addOrReplaceOne() example
<?php
/*
Assuming that $coll is a valid collection object, the following call to addOrReplaceOne
will search for a document with the ID 121 and will either replace it or add a new one with
that ID.
*/
$res = $coll->addOrReplaceOne('121', '{"name": "Sakila", "age": 30, "job": "Radio", "_id": "121" }');
?>