-
Notifications
You must be signed in to change notification settings - Fork 34
DOCSP-41969: Replace documents #125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
norareidy
merged 6 commits into
mongodb:php-standardization
from
norareidy:DOCSP-41969-replace
Sep 10, 2024
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3fa67b8
DOCSP-41969: Replace documents
norareidy 68916f4
edits
norareidy e98b209
wording
norareidy 0b19f94
SA
norareidy 833def8
JT feedback
norareidy 4494cdf
Merge remote-tracking branch 'upstream/php-standardization' into DOCS…
norareidy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
require 'vendor/autoload.php'; | ||
|
||
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset'); | ||
$client = new MongoDB\Client($uri); | ||
|
||
// start-db-coll | ||
$collection = $client->sample_restaurants->restaurants; | ||
// end-db-coll | ||
|
||
// start-replace-one | ||
$replace_document = [ | ||
'name' => 'Mongo\'s Pizza', | ||
'cuisine' => 'Pizza', | ||
'address' => [ | ||
'street' => '123 Pizza St', | ||
'zipCode' => '10003', | ||
], | ||
'borough' => 'Manhattan' | ||
]; | ||
|
||
$result = $collection->replaceOne(['name' => 'Pizza Town'], $replace_document); | ||
echo 'Modified documents: ' . $result->getModifiedCount(); | ||
// end-replace-one | ||
|
||
// start-replace-options | ||
$replace_document = [ | ||
'name' => 'Food World', | ||
'cuisine' => 'Mixed', | ||
'address' => [ | ||
'street' => '123 Food St', | ||
'zipCode' => '10003', | ||
], | ||
'borough' => 'Manhattan' | ||
]; | ||
|
||
$result = $collection->replaceOne( | ||
['name' => 'Food Town'], | ||
$replace_document, | ||
['upsert' => true] | ||
); | ||
// end-replace-options |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
.. _php-write: | ||
|
||
===================== | ||
Write Data to MongoDB | ||
===================== | ||
|
||
.. toctree:: | ||
:titlesonly: | ||
:maxdepth: 1 | ||
|
||
/write/replace |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
.. _php-write-replace: | ||
|
||
================= | ||
Replace Documents | ||
================= | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: modify, change, code example | ||
|
||
Overview | ||
-------- | ||
|
||
In this guide, you can learn how to use the {+php-library+} to run a replace | ||
operation on a MongoDB collection. A replace operation performs differently | ||
than an update operation. An update operation modifies only the specified | ||
fields in a target document. A replace operation removes *all* fields | ||
in the target document and replaces them with new ones. | ||
|
||
To replace a document, use the ``MongoDB\Collection::replaceOne()`` method. | ||
|
||
Sample Data | ||
~~~~~~~~~~~ | ||
|
||
The examples in this guide use the ``restaurants`` collection in the ``sample_restaurants`` | ||
database from the :atlas:`Atlas sample datasets </sample-data>`. To access this collection | ||
from your PHP application, instantiate a ``MongoDB\Client`` that connects to an Atlas cluster | ||
and assign the following value to your ``$collection`` variable: | ||
|
||
.. literalinclude:: /includes/read/project.php | ||
:language: php | ||
:dedent: | ||
:start-after: start-db-coll | ||
:end-before: end-db-coll | ||
|
||
To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the | ||
:atlas:`Get Started with Atlas </getting-started>` guide. | ||
|
||
Replace Operation | ||
----------------- | ||
|
||
You can perform a replace operation by using the ``MongoDB\Collection::replaceOne()`` method. | ||
This method removes all fields except the ``_id`` field from the first document that | ||
matches the search criteria. It then inserts the fields and values you specify into the | ||
document. | ||
|
||
Required Parameters | ||
~~~~~~~~~~~~~~~~~~~ | ||
|
||
The ``replaceOne()`` method requires the following parameters: | ||
|
||
- **Query filter** document, which determines the documents to replace. For | ||
more information about query filters, see the | ||
:manual:`Query Filter Documents section </core/document/#query-filter-documents>` in | ||
the {+mdb-server+} manual. | ||
- **Replace** document, which specifies the fields and values to insert in the new | ||
document. | ||
|
||
Return Value | ||
~~~~~~~~~~~~ | ||
|
||
The ``replaceOne()`` method returns a ``MongoDB\UpdateResult`` | ||
object. The ``MongoDB\UpdateResult`` type contains the following | ||
methods: | ||
|
||
.. list-table:: | ||
:widths: 30 70 | ||
:header-rows: 1 | ||
|
||
* - Method | ||
- Description | ||
|
||
* - ``getMatchedCount()`` | ||
- | Returns the number of documents that matched the query filter, regardless of | ||
how many were updated. | ||
|
||
* - ``getModifiedCount()`` | ||
- | Returns the number of documents modified by the update operation. If an updated | ||
document is identical to the original, it is not included in this | ||
count. | ||
|
||
* - ``getUpsertedCount()`` | ||
- | Returns the number of documents upserted into the database, if any. | ||
|
||
* - ``getUpsertedId()`` | ||
- | Returns the ID of the document that was upserted in the database, if the driver | ||
performed an upsert. | ||
|
||
* - ``isAcknowledged()`` | ||
- | Returns a boolean indicating whether the write operation was acknowledged. | ||
|
||
Example | ||
~~~~~~~ | ||
|
||
The following example uses the ``replaceOne()`` method to replace the fields and values | ||
of a document in which the ``name`` field value is ``'Pizza Town'``. It then prints | ||
the number of modified documents: | ||
|
||
.. io-code-block:: | ||
:copyable: | ||
|
||
.. input:: /includes/write/replace.php | ||
:start-after: start-replace-one | ||
:end-before: end-replace-one | ||
:language: php | ||
:dedent: | ||
|
||
.. output:: | ||
:visible: false | ||
|
||
Modified documents: 1 | ||
|
||
.. important:: | ||
|
||
The values of ``_id`` fields are immutable. If your replacement document specifies | ||
a value for the ``_id`` field, it must match the ``_id`` value of the existing document. | ||
|
||
Modify the Replace Operation | ||
---------------------------- | ||
|
||
You can modify the behavior of the ``MongoDB\Collection::replaceOne()`` method | ||
by passing an array that specifies option values as a parameter. The following | ||
table describes some options you can set in the array: | ||
|
||
.. list-table:: | ||
:widths: 30 70 | ||
:header-rows: 1 | ||
|
||
* - Option | ||
- Description | ||
|
||
* - ``upsert`` | ||
- | Specifies whether the replace operation performs an upsert operation if no | ||
documents match the query filter. For more information, see the :manual:`upsert | ||
statement </reference/command/update/#std-label-update-command-upsert>` | ||
in the {+mdb-server+} manual. | ||
| Defaults to ``false``. | ||
|
||
* - ``bypassDocumentValidation`` | ||
- | Specifies whether the replace operation bypasses document validation. This lets you | ||
replace documents that don't meet the schema validation requirements, if any | ||
exist. For more information about schema validation, see :manual:`Schema | ||
Validation </core/schema-validation/#schema-validation>` in the MongoDB | ||
Server manual. | ||
| Defaults to ``false``. | ||
|
||
* - ``collation`` | ||
- | Specifies the kind of language collation to use when sorting | ||
results. For more information, see :manual:`Collation </reference/collation/#std-label-collation>` | ||
in the {+mdb-server+} manual. | ||
|
||
* - ``hint`` | ||
- | Gets or sets the index to scan for documents. | ||
For more information, see the :manual:`hint statement </reference/command/update/#std-label-update-command-hint>` | ||
in the {+mdb-server+} manual. | ||
|
||
* - ``session`` | ||
- | Specifies the client session to associate with the operation. | ||
|
||
* - ``let`` | ||
- | Specifies a document with a list of values to improve operation readability. | ||
Values must be constant or closed expressions that don't reference document | ||
fields. For more information, see the :manual:`let statement | ||
</reference/command/update/#std-label-update-let-syntax>` in the | ||
{+mdb-server+} manual. | ||
|
||
* - ``comment`` | ||
- | Attaches a comment to the operation. For more information, see the :manual:`insert command | ||
fields </reference/command/insert/#command-fields>` guide in the | ||
{+mdb-server+} manual. | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Example | ||
~~~~~~~ | ||
|
||
The following code uses the ``replaceOne()`` method to find the first document | ||
in which the ``name`` field has the value ``'Food Town'``, then replaces this document | ||
with a new document in which the ``name`` value is ``'Food World'``. Because the | ||
``upsert`` option is set to ``true``, the library inserts a new document if the query | ||
filter doesn't match any existing documents: | ||
|
||
.. literalinclude:: /includes/write/replace.php | ||
:start-after: start-replace-options | ||
:end-before: end-replace-options | ||
:language: php | ||
:copyable: | ||
|
||
Additional Information | ||
---------------------- | ||
|
||
To learn more about update operations, see the :ref:`php-write-update` guide. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note to reviewer: link broken until #118 is merged |
||
|
||
To learn more about creating query filters, see the :ref:`php-specify-query` guide. | ||
|
||
API Documentation | ||
~~~~~~~~~~~~~~~~~ | ||
|
||
To learn more about any of the methods or types discussed in this | ||
guide, see the following API documentation: | ||
|
||
- `MongoDB\\Collection::replaceOne() <{+api+}/method/MongoDBCollection-replaceOne/>`__ | ||
- `MongoDB\\UpdateResult <{+api+}/class/MongoDBUpdateResult/>`__ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.