@@ -9,7 +9,7 @@ Write Operations
9
9
:values: tutorial
10
10
11
11
.. meta::
12
- :keywords: insert, insert one, update, update one, upsert, code example, mass assignment, eloquent model
12
+ :keywords: insert, insert one, update, update one, upsert, code example, mass assignment, push, pull, eloquent model
13
13
14
14
.. contents:: On this page
15
15
:local:
@@ -167,6 +167,7 @@ This section provides examples of the following update operations:
167
167
- :ref:`Update a document <laravel-modify-documents-update-one>`
168
168
- :ref:`Update multiple documents <laravel-modify-documents-update-multiple>`
169
169
- :ref:`Update or insert in a single operation <laravel-modify-documents-upsert>`
170
+ - :ref:`Update arrays in a document <laravel-modify-documents-arrays>`
170
171
171
172
.. _laravel-modify-documents-update-one:
172
173
@@ -292,7 +293,6 @@ matching documents exist:
292
293
.. input:: /includes/fundamentals/write-operations/WriteOperationsTest.php
293
294
:language: php
294
295
:dedent:
295
- :emphasize-lines: 4
296
296
:start-after: begin model upsert
297
297
:end-before: end model upsert
298
298
@@ -311,3 +311,172 @@ matching documents exist:
311
311
"ticketsSold": 4000,
312
312
"updated_at": ...
313
313
}
314
+
315
+ .. _laravel-modify-documents-arrays:
316
+
317
+ Update Arrays in a Document
318
+ ---------------------------
319
+
320
+ In this section, you can see examples of the following operations that
321
+ update array values in a MongoDB document:
322
+
323
+ - :ref:`Add values to an array <laravel-modify-documents-add-array-values>`
324
+ - :ref:`Remove values from an array <laravel-modify-documents-remove-array-values>`
325
+ - :ref:`Update the value of an array element <laravel-modify-documents-update-array-values>`
326
+
327
+ These examples modify the sample document created by the following insert
328
+ operation:
329
+
330
+ .. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php
331
+ :language: php
332
+ :dedent:
333
+ :start-after: begin array example document
334
+ :end-before: end array example document
335
+
336
+ .. _laravel-modify-documents-add-array-values:
337
+
338
+ Add Values to an Array Example
339
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
340
+
341
+ This section shows how to use the ``push()`` method to add values to an array
342
+ in a MongoDB document. You can pass one or more values to add and set the
343
+ optional parameter ``unique`` to ``true`` to skip adding any duplicate values
344
+ in the array. The following code example shows the structure of a ``push()``
345
+ method call:
346
+
347
+ .. code-block:: none
348
+ :copyable: false
349
+
350
+ YourModel::where(<match criteria>)
351
+ ->push(
352
+ <field name>,
353
+ [<values>], // array or single value to add
354
+ unique: true); // whether to skip existing values
355
+
356
+ The following example shows how to add the value ``"baroque"`` to
357
+ the ``genres`` array field of a matching document. Click the
358
+ :guilabel:`VIEW OUTPUT` button to see the updated document:
359
+
360
+ .. io-code-block::
361
+
362
+ .. input:: /includes/fundamentals/write-operations/WriteOperationsTest.php
363
+ :language: php
364
+ :dedent:
365
+ :start-after: begin model array push
366
+ :end-before: end model array push
367
+
368
+ .. output::
369
+ :language: json
370
+ :visible: false
371
+
372
+ {
373
+ "_id": "660eb...",
374
+ "performer": "Mitsuko Uchida",
375
+ "genres": [
376
+ "classical",
377
+ "dance-pop",
378
+
379
+ ],
380
+ "updated_at": ...,
381
+ "created_at": ...
382
+ }
383
+
384
+
385
+ .. _laravel-modify-documents-remove-array-values:
386
+
387
+ Remove Values From an Array Example
388
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
389
+
390
+ This section shows how to use the ``pull()`` method to remove values from
391
+ an array in a MongoDB document. You can pass one or more values to remove
392
+ from the array. The following code example shows the structure of a
393
+ ``pull()`` method call:
394
+
395
+ .. code-block:: none
396
+ :copyable: false
397
+
398
+ YourModel::where(<match criteria>)
399
+ ->pull(
400
+ <field name>,
401
+ [<values>]); // array or single value to remove
402
+
403
+ The following example shows how to remove array values ``"classical"`` and
404
+ ``"dance-pop"`` from the ``genres`` array field. Click the
405
+ :guilabel:`VIEW OUTPUT` button to see the updated document:
406
+
407
+ .. io-code-block::
408
+
409
+ .. input:: /includes/fundamentals/write-operations/WriteOperationsTest.php
410
+ :language: php
411
+ :dedent:
412
+ :start-after: begin model array pull
413
+ :end-before: end model array pull
414
+
415
+ .. output::
416
+ :language: json
417
+ :visible: false
418
+
419
+ {
420
+ "_id": "660e...",
421
+ "performer": "Mitsuko Uchida",
422
+ "genres": [],
423
+ "updated_at": ...,
424
+ "created_at": ...
425
+ }
426
+
427
+
428
+ .. _laravel-modify-documents-update-array-values:
429
+
430
+ Update the Value of an Array Element Example
431
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
432
+
433
+ This section shows how to use the ``$`` positional operator to update specific
434
+ array elements in a MongoDB document. The ``$`` operator represents the first
435
+ array element that matches the query. The following code example shows the
436
+ structure of a positional operator update call on a single matching document:
437
+
438
+
439
+ .. note::
440
+
441
+ Currently, {+odm-short+} offers this operation only on the ``DB`` facade
442
+ and not on the Eloquent ORM.
443
+
444
+ .. code-block:: none
445
+ :copyable: false
446
+
447
+ DB::connection('mongodb')
448
+ ->getCollection(<collection name>)
449
+ ->updateOne(
450
+ <match criteria>,
451
+ ['$set' => ['<array field>.$' => <replacement value>]]);
452
+
453
+
454
+ The following example shows how to replace the array value ``"dance-pop"``
455
+ with ``"contemporary"`` in the ``genres`` array field. Click the
456
+ :guilabel:`VIEW OUTPUT` button to see the updated document:
457
+
458
+ .. io-code-block::
459
+
460
+ .. input:: /includes/fundamentals/write-operations/WriteOperationsTest.php
461
+ :language: php
462
+ :dedent:
463
+ :start-after: begin model array positional
464
+ :end-before: end model array positional
465
+
466
+ .. output::
467
+ :language: json
468
+ :visible: false
469
+
470
+ {
471
+ "_id": "660e...",
472
+ "performer": "Mitsuko Uchida",
473
+ "genres": [
474
+ "classical",
475
+ "contemporary"
476
+ ],
477
+ "updated_at": ...,
478
+ "created_at": ...
479
+ }
480
+
481
+ To learn more about array update operators, see :manual:`Array Update Operators </reference/operator/update-array/>`
482
+ in the {+server-docs-name+}.
0 commit comments