-
Notifications
You must be signed in to change notification settings - Fork 1.4k
DOCSP-35946 write operations arrays #2825
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
ccho-mongodb
merged 32 commits into
mongodb:4.1-base-docs-write-operations
from
ccho-mongodb:DOCSP-35946-write-operations-arrays
Apr 8, 2024
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
8fbbcc4
DOCSP-35948: Write operations
a5a75ca
add examples
a678179
apply phpcbf formatting
ccho-mongodb eea96e5
wip
88024a5
apply phpcbf formatting
ccho-mongodb 24fdd13
add date info
77bdbec
apply phpcbf formatting
ccho-mongodb 831f06e
update title of read operations
d813f23
grammar fixes
a194e60
DOCSP-35947: write operations - modify documents
2582bd5
apply phpcbf formatting
ccho-mongodb 86ff663
misc fixes
4dc106a
rst fixes
678490e
fixes
8ff6872
fix
44eb655
fix
60a6cfb
fix rst
a9d072c
concise sentence
79037d2
linebreaks
78bc0e7
double backslash
1d77153
fix
39eb362
DOCSP-35949: Write Operations - Upsert
dfe2019
apply phpcbf formatting
ccho-mongodb ebf1cc5
fix io-codeblock
2c339bd
tweaks
f2b0412
apply phpcbf formatting
ccho-mongodb cd7f08a
DOCSP-35946: write operations arrays
9ba28be
fixes
07530e6
remove emphasize lines
e6c162f
PRR fixes
da06e94
Merge branch '4.1-base-docs-write-operations' into DOCSP-35946-write-…
9be7a28
Merge branch '4.1-base-docs-write-operations' into DOCSP-35946-write-…
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 | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,7 +9,7 @@ Write Operations | |||||||||
| :values: tutorial | ||||||||||
|
|
||||||||||
| .. meta:: | ||||||||||
| :keywords: insert, insert one, update, update one, upsert, code example, mass assignment, eloquent model | ||||||||||
| :keywords: insert, insert one, update, update one, upsert, code example, mass assignment, push, pull, eloquent model | ||||||||||
|
|
||||||||||
| .. contents:: On this page | ||||||||||
| :local: | ||||||||||
|
|
@@ -167,6 +167,7 @@ This section provides examples of the following update operations: | |||||||||
| - :ref:`Update a document <laravel-modify-documents-update-one>` | ||||||||||
| - :ref:`Update multiple documents <laravel-modify-documents-update-multiple>` | ||||||||||
| - :ref:`Update or insert in a single operation <laravel-modify-documents-upsert>` | ||||||||||
| - :ref:`Update arrays in a document <laravel-modify-documents-arrays>` | ||||||||||
|
|
||||||||||
| .. _laravel-modify-documents-update-one: | ||||||||||
|
|
||||||||||
|
|
@@ -292,7 +293,6 @@ matching documents exist: | |||||||||
| .. input:: /includes/fundamentals/write-operations/WriteOperationsTest.php | ||||||||||
| :language: php | ||||||||||
| :dedent: | ||||||||||
| :emphasize-lines: 4 | ||||||||||
| :start-after: begin model upsert | ||||||||||
| :end-before: end model upsert | ||||||||||
|
|
||||||||||
|
|
@@ -311,3 +311,172 @@ matching documents exist: | |||||||||
| "ticketsSold": 4000, | ||||||||||
| "updated_at": ... | ||||||||||
| } | ||||||||||
|
|
||||||||||
| .. _laravel-modify-documents-arrays: | ||||||||||
|
|
||||||||||
| Update Arrays in a Document | ||||||||||
| --------------------------- | ||||||||||
|
|
||||||||||
| In this section, you can see examples of the following operations that | ||||||||||
| update array values in a MongoDB document: | ||||||||||
|
|
||||||||||
| - :ref:`Add values to an array <laravel-modify-documents-add-array-values>` | ||||||||||
| - :ref:`Remove values from an array <laravel-modify-documents-remove-array-values>` | ||||||||||
| - :ref:`Update the value of an array element <laravel-modify-documents-update-array-values>` | ||||||||||
|
|
||||||||||
| These examples modify the sample document created by the following insert | ||||||||||
| operation: | ||||||||||
|
|
||||||||||
| .. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php | ||||||||||
| :language: php | ||||||||||
| :dedent: | ||||||||||
| :start-after: begin array example document | ||||||||||
| :end-before: end array example document | ||||||||||
|
|
||||||||||
| .. _laravel-modify-documents-add-array-values: | ||||||||||
|
|
||||||||||
| Add Values to an Array Example | ||||||||||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||
|
|
||||||||||
| This section shows how to use the ``push()`` method to add values to an array | ||||||||||
| in a MongoDB document. You can pass one or more values to add and set the | ||||||||||
| optional parameter ``unique`` to ``true`` to skip adding any duplicate values | ||||||||||
| in the array. The following code example shows the structure of a ``push()`` | ||||||||||
| method call: | ||||||||||
|
|
||||||||||
| .. code-block:: none | ||||||||||
| :copyable: false | ||||||||||
|
|
||||||||||
| YourModel::where(<match criteria>) | ||||||||||
| ->push( | ||||||||||
| <field name>, | ||||||||||
| [<values>], // array or single value to add | ||||||||||
| unique: true); // whether to skip existing values | ||||||||||
|
|
||||||||||
| The following example shows how to add the value ``"baroque"`` to | ||||||||||
| the ``genres`` array field of a matching document. Click the | ||||||||||
| :guilabel:`VIEW OUTPUT` button to see the updated document: | ||||||||||
|
|
||||||||||
| .. io-code-block:: | ||||||||||
|
|
||||||||||
| .. input:: /includes/fundamentals/write-operations/WriteOperationsTest.php | ||||||||||
| :language: php | ||||||||||
| :dedent: | ||||||||||
| :start-after: begin model array push | ||||||||||
| :end-before: end model array push | ||||||||||
|
|
||||||||||
| .. output:: | ||||||||||
| :language: json | ||||||||||
| :visible: false | ||||||||||
|
|
||||||||||
| { | ||||||||||
| "_id": "660eb...", | ||||||||||
| "performer": "Mitsuko Uchida", | ||||||||||
| "genres": [ | ||||||||||
| "classical", | ||||||||||
| "dance-pop", | ||||||||||
|
|
||||||||||
| ], | ||||||||||
| "updated_at": ..., | ||||||||||
| "created_at": ... | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
||||||||||
| .. _laravel-modify-documents-remove-array-values: | ||||||||||
|
|
||||||||||
| Remove Values From an Array Example | ||||||||||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||
|
|
||||||||||
| This section shows how to use the ``pull()`` method to remove values from | ||||||||||
| an array in a MongoDB document. You can pass one or more values to remove | ||||||||||
| from the array. The following code example shows the structure of a | ||||||||||
| ``pull()`` method call: | ||||||||||
|
|
||||||||||
| .. code-block:: none | ||||||||||
| :copyable: false | ||||||||||
|
|
||||||||||
| YourModel::where(<match criteria>) | ||||||||||
| ->pull( | ||||||||||
| <field name>, | ||||||||||
| [<values>]); // array or single value to remove | ||||||||||
|
|
||||||||||
| The following example shows how to remove array values ``"classical"`` and | ||||||||||
| ``"dance-pop"`` from the ``genres`` array field. Click the | ||||||||||
| :guilabel:`VIEW OUTPUT` button to see the updated document: | ||||||||||
|
|
||||||||||
| .. io-code-block:: | ||||||||||
|
|
||||||||||
| .. input:: /includes/fundamentals/write-operations/WriteOperationsTest.php | ||||||||||
| :language: php | ||||||||||
| :dedent: | ||||||||||
| :start-after: begin model array pull | ||||||||||
| :end-before: end model array pull | ||||||||||
|
|
||||||||||
| .. output:: | ||||||||||
| :language: json | ||||||||||
| :visible: false | ||||||||||
|
|
||||||||||
| { | ||||||||||
| "_id": "660e...", | ||||||||||
| "performer": "Mitsuko Uchida", | ||||||||||
| "genres": [], | ||||||||||
| "updated_at": ..., | ||||||||||
| "created_at": ... | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
||||||||||
| .. _laravel-modify-documents-update-array-values: | ||||||||||
|
|
||||||||||
| Update the Value of an Array Element Example | ||||||||||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||
|
|
||||||||||
| This section shows how to use the ``$`` positional operator to update specific | ||||||||||
| array elements in a MongoDB document. The ``$`` operator represents the first | ||||||||||
| array element that matches the query. The following code example shows the | ||||||||||
| structure of a positional operator update call on a single matching document: | ||||||||||
|
|
||||||||||
|
|
||||||||||
| .. note:: | ||||||||||
|
|
||||||||||
| Currently, {+odm-short+} offers this operation only on the ``DB`` facade | ||||||||||
| and not on the Eloquent ORM. | ||||||||||
|
|
||||||||||
| .. code-block:: none | ||||||||||
| :copyable: false | ||||||||||
|
|
||||||||||
| DB::connection('mongodb') | ||||||||||
| ->getCollection(<collection name>) | ||||||||||
| ->updateOne( | ||||||||||
| <match criteria>, | ||||||||||
| ['$set' => ['<array field>.$' => <replacement value>]]); | ||||||||||
|
|
||||||||||
|
|
||||||||||
| The following example shows how to replace the array value ``"dance-pop"`` | ||||||||||
| with ``"contemporary"`` in the ``genres`` array field. Click the | ||||||||||
|
Comment on lines
+454
to
+455
Contributor
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. S: same comment about single quotes as above:
Suggested change
|
||||||||||
| :guilabel:`VIEW OUTPUT` button to see the updated document: | ||||||||||
|
|
||||||||||
| .. io-code-block:: | ||||||||||
|
|
||||||||||
| .. input:: /includes/fundamentals/write-operations/WriteOperationsTest.php | ||||||||||
| :language: php | ||||||||||
| :dedent: | ||||||||||
| :start-after: begin model array positional | ||||||||||
| :end-before: end model array positional | ||||||||||
|
|
||||||||||
| .. output:: | ||||||||||
| :language: json | ||||||||||
| :visible: false | ||||||||||
|
|
||||||||||
| { | ||||||||||
| "_id": "660e...", | ||||||||||
| "performer": "Mitsuko Uchida", | ||||||||||
| "genres": [ | ||||||||||
| "classical", | ||||||||||
| "contemporary" | ||||||||||
| ], | ||||||||||
| "updated_at": ..., | ||||||||||
| "created_at": ... | ||||||||||
| } | ||||||||||
|
|
||||||||||
| To learn more about array update operators, see :manual:`Array Update Operators </reference/operator/update-array/>` | ||||||||||
| in the {+server-docs-name+}. | ||||||||||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
S: [nit] since single quotes are used in the code: