@@ -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, push, pull, eloquent model
12
+ :keywords: insert, insert one, update, update one, upsert, code example, mass assignment, push, pull, delete, delete many, primary key, destroy, eloquent model
13
13
14
14
.. contents:: On this page
15
15
:local:
@@ -28,7 +28,8 @@ This guide shows you how to perform the following tasks:
28
28
29
29
- :ref:`laravel-fundamentals-insert-documents`
30
30
- :ref:`laravel-fundamentals-modify-documents`
31
- - Delete Documents
31
+ - :ref:`laravel-fundamentals-delete-documents`
32
+
32
33
33
34
.. _laravel-fundamentals-write-sample-model:
34
35
@@ -98,6 +99,7 @@ This example code performs the following actions:
98
99
:dedent:
99
100
:start-after: begin model insert one
100
101
:end-before: end model insert one
102
+ :caption: Insert a document by calling the save() method on an instance.
101
103
102
104
You can retrieve the inserted document's ``_id`` value by accessing the model's
103
105
``id`` member, as shown in the following code example:
@@ -189,6 +191,7 @@ of the model and calling its ``save()`` method:
189
191
:dedent:
190
192
:start-after: begin model update one save
191
193
:end-before: end model update one save
194
+ :caption: Update a document by calling the save() method on an instance.
192
195
193
196
When the ``save()`` method succeeds, the model instance on which you called the
194
197
method contains the updated values.
@@ -203,13 +206,9 @@ retrieve and update the first matching document:
203
206
:dedent:
204
207
:start-after: begin model update one fluent
205
208
:end-before: end model update one fluent
209
+ :caption: Update the matching document by chaining the update() method.
206
210
207
- .. note::
208
-
209
- The ``orderBy()`` call sorts the results by the ``_id`` field to
210
- guarantee a consistent sort order. To learn more about sorting in MongoDB,
211
- see the :manual:`Natural order </reference/glossary/#std-term-natural-order>`
212
- glossary entry in the {+server-docs-name+}.
211
+ .. include:: /includes/fact-orderby-id.rst
213
212
214
213
When the ``update()`` method succeeds, the operation returns the number of
215
214
documents updated.
@@ -480,3 +479,139 @@ with ``"contemporary"`` in the ``genres`` array field. Click the
480
479
481
480
To learn more about array update operators, see :manual:`Array Update Operators </reference/operator/update-array/>`
482
481
in the {+server-docs-name+}.
482
+
483
+ .. _laravel-fundamentals-delete-documents:
484
+
485
+ Delete Documents
486
+ ----------------
487
+
488
+ In this section, you can learn how to delete documents from a MongoDB collection
489
+ by using {+odm-short+}. Use delete operations to remove data from your MongoDB
490
+ database.
491
+
492
+ This section provides examples of the following delete operations:
493
+
494
+ - :ref:`Delete one document <laravel-fundamentals-delete-one>`
495
+ - :ref:`Delete multiple documents <laravel-fundamentals-delete-many>`
496
+
497
+ To learn about the Laravel features available in {+odm-short+} that modify
498
+ delete behavior, see the following sections:
499
+
500
+ - :ref:`Soft delete <laravel-model-soft-delete>`, which lets you mark
501
+ documents as deleted instead of removing them from the database
502
+ - :ref:`Pruning <laravel-model-pruning>`, which lets you define conditions
503
+ that qualify a document for automatic deletion
504
+
505
+ .. _laravel-fundamentals-delete-one:
506
+
507
+ Delete a Document Examples
508
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
509
+
510
+ You can delete one document in the following ways:
511
+
512
+ - Call the ``$model->delete()`` method on an instance of the model.
513
+ - Call the ``Model::destroy($id)`` method on the model, passing it the id of
514
+ the document to be deleted.
515
+ - Chain methods to retrieve and delete an instance of a model by calling the
516
+ ``delete()`` method.
517
+
518
+ The following example shows how to delete a document by calling ``$model->delete()``
519
+ on an instance of the model:
520
+
521
+ .. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php
522
+ :language: php
523
+ :dedent:
524
+ :start-after: begin delete one model
525
+ :end-before: end delete one model
526
+ :caption: Delete the document by calling the delete() method on an instance.
527
+
528
+ When the ``delete()`` method succeeds, the operation returns the number of
529
+ documents deleted.
530
+
531
+ If the retrieve part of the call does not match any documents in the collection,
532
+ the operation returns ``0``.
533
+
534
+ The following example shows how to delete a document by passing the value of
535
+ its id to the ``Model::destroy($id)`` method:
536
+
537
+ .. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php
538
+ :language: php
539
+ :dedent:
540
+ :start-after: begin model delete by id
541
+ :end-before: end model delete by id
542
+ :caption: Delete the document by its id value.
543
+
544
+ When the ``destroy()`` method succeeds, it returns the number of documents
545
+ deleted.
546
+
547
+ If the id value does not match any documents, the ``destroy()`` method
548
+ returns returns ``0``.
549
+
550
+ The following example shows how to chain calls to retrieve the first
551
+ matching document and delete it:
552
+
553
+ .. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php
554
+ :language: php
555
+ :dedent:
556
+ :start-after: begin model delete one fluent
557
+ :end-before: end model delete one fluent
558
+ :caption: Delete the matching document by chaining the delete() method.
559
+
560
+ .. include:: /includes/fact-orderby-id.rst
561
+
562
+ When the ``delete()`` method succeeds, it returns the number of documents
563
+ deleted.
564
+
565
+ If the ``where()`` method does not match any documents, the ``delete()`` method
566
+ returns returns ``0``.
567
+
568
+ .. _laravel-fundamentals-delete-many:
569
+
570
+ Delete Multiple Documents Examples
571
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
572
+
573
+ You can delete multiple documents in the following ways:
574
+
575
+
576
+ - Call the ``Model::destroy($ids)`` method, passing a list of the ids of the
577
+ documents or model instances to be deleted.
578
+ - Chain methods to retrieve a Laravel collection object that references
579
+ multiple objects and delete them by calling the ``delete()`` method.
580
+
581
+ The following example shows how to delete a document by passing an array of
582
+ id values, represented by ``$ids``, to the ``destroy()`` method:
583
+
584
+ .. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php
585
+ :language: php
586
+ :dedent:
587
+ :start-after: begin model delete multiple by id
588
+ :end-before: end model delete multiple by id
589
+ :caption: Delete documents by their ids.
590
+
591
+ .. tip::
592
+
593
+ The ``destroy()`` method performance suffers when passed large lists. For
594
+ better performance, use ``Model::whereIn('id', $ids)->delete()`` instead.
595
+
596
+ When the ``destroy()`` method succeeds, it returns the number of documents
597
+ deleted.
598
+
599
+ If the id values do not match any documents, the ``destroy()`` method
600
+ returns returns ``0``.
601
+
602
+ The following example shows how to chain calls to retrieve matching documents
603
+ and delete them:
604
+
605
+ .. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php
606
+ :language: php
607
+ :dedent:
608
+ :start-after: begin model delete multiple fluent
609
+ :end-before: end model delete multiple fluent
610
+ :caption: Chain calls to retrieve matching documents and delete them.
611
+
612
+ When the ``delete()`` method succeeds, it returns the number of documents
613
+ deleted.
614
+
615
+ If the ``where()`` method does not match any documents, the ``delete()`` method
616
+ returns ``0``.
617
+
0 commit comments