Skip to content

DOCSP-35951: write operations delete docs #2829

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
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
8fbbcc4
DOCSP-35948: Write operations
Mar 27, 2024
a5a75ca
add examples
Mar 28, 2024
a678179
apply phpcbf formatting
ccho-mongodb Mar 28, 2024
eea96e5
wip
Mar 28, 2024
88024a5
apply phpcbf formatting
ccho-mongodb Mar 28, 2024
24fdd13
add date info
Mar 29, 2024
77bdbec
apply phpcbf formatting
ccho-mongodb Mar 29, 2024
831f06e
update title of read operations
Mar 29, 2024
d813f23
grammar fixes
Mar 29, 2024
a194e60
DOCSP-35947: write operations - modify documents
Apr 1, 2024
2582bd5
apply phpcbf formatting
ccho-mongodb Apr 1, 2024
86ff663
misc fixes
Apr 1, 2024
4dc106a
rst fixes
Apr 1, 2024
678490e
fixes
Apr 1, 2024
8ff6872
fix
Apr 1, 2024
44eb655
fix
Apr 1, 2024
60a6cfb
fix rst
Apr 1, 2024
a9d072c
concise sentence
Apr 1, 2024
79037d2
linebreaks
Apr 1, 2024
78bc0e7
double backslash
Apr 1, 2024
1d77153
fix
Apr 1, 2024
39eb362
DOCSP-35949: Write Operations - Upsert
Apr 2, 2024
dfe2019
apply phpcbf formatting
ccho-mongodb Apr 2, 2024
ebf1cc5
fix io-codeblock
Apr 2, 2024
2c339bd
tweaks
Apr 2, 2024
f2b0412
apply phpcbf formatting
ccho-mongodb Apr 2, 2024
cd7f08a
DOCSP-35946: write operations arrays
Apr 3, 2024
9ba28be
fixes
Apr 4, 2024
07530e6
remove emphasize lines
Apr 4, 2024
e6c162f
PRR fixes
Apr 4, 2024
4aa7e5c
DOCSP-35951: Write operations - delete documents
Apr 8, 2024
bb3439b
add include, fix comment
Apr 8, 2024
25a3912
fixes
Apr 8, 2024
868d3a6
fixes
Apr 8, 2024
aee0efe
fix
Apr 8, 2024
2512f78
another fix
Apr 8, 2024
78df112
fix stray text
Apr 8, 2024
a69a0e7
PRR fixes
Apr 9, 2024
022622f
Merge branch '4.1-base-docs-write-operations' into DOCSP-35951-write-…
Apr 9, 2024
1018c36
PRR fixes
Apr 10, 2024
af90cb7
PRR fixes
Apr 12, 2024
8830948
PRR fixes - remove unused variables
Apr 12, 2024
d07cb68
update wording for delete operations
Apr 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 143 additions & 8 deletions docs/fundamentals/write-operations.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Write Operations
:values: tutorial

.. meta::
:keywords: insert, insert one, update, update one, upsert, code example, mass assignment, push, pull, eloquent model
:keywords: insert, insert one, update, update one, upsert, code example, mass assignment, push, pull, delete, delete many, primary key, destroy, eloquent model

.. contents:: On this page
:local:
Expand All @@ -28,7 +28,8 @@ This guide shows you how to perform the following tasks:

- :ref:`laravel-fundamentals-insert-documents`
- :ref:`laravel-fundamentals-modify-documents`
- Delete Documents
- :ref:`laravel-fundamentals-delete-documents`


.. _laravel-fundamentals-write-sample-model:

Expand Down Expand Up @@ -98,6 +99,7 @@ This example code performs the following actions:
:dedent:
:start-after: begin model insert one
:end-before: end model insert one
:caption: Insert a document by calling the save() method on an instance.

You can retrieve the inserted document's ``_id`` value by accessing the model's
``id`` member, as shown in the following code example:
Expand Down Expand Up @@ -189,6 +191,7 @@ of the model and calling its ``save()`` method:
:dedent:
:start-after: begin model update one save
:end-before: end model update one save
:caption: Update a document by calling the save() method on an instance.

When the ``save()`` method succeeds, the model instance on which you called the
method contains the updated values.
Expand All @@ -203,13 +206,9 @@ retrieve and update the first matching document:
:dedent:
:start-after: begin model update one fluent
:end-before: end model update one fluent
:caption: Update the matching document by chaining the update() method.

.. note::

The ``orderBy()`` call sorts the results by the ``_id`` field to
guarantee a consistent sort order. To learn more about sorting in MongoDB,
see the :manual:`Natural order </reference/glossary/#std-term-natural-order>`
glossary entry in the {+server-docs-name+}.
.. include:: /includes/fact-orderby-id.rst

When the ``update()`` method succeeds, the operation returns the number of
documents updated.
Expand Down Expand Up @@ -480,3 +479,139 @@ with ``"contemporary"`` in the ``genres`` array field. Click the

To learn more about array update operators, see :manual:`Array Update Operators </reference/operator/update-array/>`
in the {+server-docs-name+}.

.. _laravel-fundamentals-delete-documents:

Delete Documents
----------------

In this section, you can learn how to delete documents from a MongoDB collection
by using {+odm-short+}. Use delete operations to remove data from your MongoDB
database.

This section provides examples of the following delete operations:

- :ref:`Delete one document <laravel-fundamentals-delete-one>`
- :ref:`Delete multiple documents <laravel-fundamentals-delete-many>`

To learn about the Laravel features available in {+odm-short+} that modify
delete behavior, see the following sections:

- :ref:`Soft delete <laravel-model-soft-delete>`, which lets you mark
documents as deleted instead of removing them from the database
- :ref:`Pruning <laravel-model-pruning>`, which lets you define conditions
that qualify a document for automatic deletion

.. _laravel-fundamentals-delete-one:

Delete a Document Examples
~~~~~~~~~~~~~~~~~~~~~~~~~~

You can delete one document in the following ways:

- Call the ``$model->delete()`` method on an instance of the model.
- Call the ``Model::destroy($id)`` method on the model, passing it the id of
the document to be deleted.
- Chain methods to retrieve and delete an instance of a model by calling the
``delete()`` method.

The following example shows how to delete a document by calling ``$model->delete()``
on an instance of the model:

.. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php
:language: php
:dedent:
:start-after: begin delete one model
:end-before: end delete one model
:caption: Delete the document by calling the delete() method on an instance.

When the ``delete()`` method succeeds, the operation returns the number of
documents deleted.

If the retrieve part of the call does not match any documents in the collection,
the operation returns ``0``.

The following example shows how to delete a document by passing the value of
its id to the ``Model::destroy($id)`` method:

.. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php
:language: php
:dedent:
:start-after: begin model delete by id
:end-before: end model delete by id
:caption: Delete the document by its id value.

When the ``destroy()`` method succeeds, it returns the number of documents
deleted.

If the id value does not match any documents, the ``destroy()`` method
returns returns ``0``.

The following example shows how to chain calls to retrieve the first
matching document and delete it:

.. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php
:language: php
:dedent:
:start-after: begin model delete one fluent
:end-before: end model delete one fluent
:caption: Delete the matching document by chaining the delete() method.

.. include:: /includes/fact-orderby-id.rst

When the ``delete()`` method succeeds, it returns the number of documents
deleted.

If the ``where()`` method does not match any documents, the ``delete()`` method
returns returns ``0``.

.. _laravel-fundamentals-delete-many:

Delete Multiple Documents Examples
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can delete multiple documents in the following ways:


- Call the ``Model::destroy($ids)`` method, passing a list of the ids of the
documents or model instances to be deleted.
- Chain methods to retrieve a Laravel collection object that references
multiple objects and delete them by calling the ``delete()`` method.

The following example shows how to delete a document by passing an array of
id values, represented by ``$ids``, to the ``destroy()`` method:

.. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php
:language: php
:dedent:
:start-after: begin model delete multiple by id
:end-before: end model delete multiple by id
:caption: Delete documents by their ids.

.. tip::

The ``destroy()`` method performance suffers when passed large lists. For
better performance, use ``Model::whereIn('id', $ids)->delete()`` instead.

When the ``destroy()`` method succeeds, it returns the number of documents
deleted.

If the id values do not match any documents, the ``destroy()`` method
returns returns ``0``.

The following example shows how to chain calls to retrieve matching documents
and delete them:

.. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php
:language: php
:dedent:
:start-after: begin model delete multiple fluent
:end-before: end model delete multiple fluent
:caption: Chain calls to retrieve matching documents and delete them.

When the ``delete()`` method succeeds, it returns the number of documents
deleted.

If the ``where()`` method does not match any documents, the ``delete()`` method
returns ``0``.

6 changes: 6 additions & 0 deletions docs/includes/fact-orderby-id.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.. note::

The ``orderBy()`` call sorts the results by the ``_id`` field to
guarantee a consistent sort order. To learn more about sorting in MongoDB,
see the :manual:`Natural order </reference/glossary/#std-term-natural-order>`
glossary entry in the {+server-docs-name+}.
186 changes: 186 additions & 0 deletions docs/includes/fundamentals/write-operations/WriteOperationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,190 @@ public function testModelPositional(): void
$this->assertContains('contemporary', $result->genres);
$this->assertFalse(in_array('dance-pop', $result->genres));
}

public function testModelDeleteById(): void
{
require_once __DIR__ . '/Concert.php';
Concert::truncate();

$data = [
[
'_id' => 'CH-0401242000',
'performer' => 'Mitsuko Uchida',
'venue' => 'Carnegie Hall',
'genres' => ['classical'],
'ticketsSold' => 2121,
'performanceDate' => new UTCDateTime(Carbon::create(2024, 4, 1, 20, 0, 0, 'EST')),
],
[
'_id' => 'MSG-0212252000',
'performer' => 'Brad Mehldau',
'venue' => 'Philharmonie de Paris',
'genres' => [ 'jazz', 'post-bop' ],
'ticketsSold' => 5745,
'performanceDate' => new UTCDateTime(Carbon::create(2025, 2, 12, 20, 0, 0, 'CET')),
],
[
'_id' => 'MSG-021222000',
'performer' => 'Billy Joel',
'venue' => 'Madison Square Garden',
'genres' => [ 'rock', 'soft rock', 'pop rock' ],
'ticketsSold' => 12852,
'performanceDate' => new UTCDateTime(Carbon::create(2025, 2, 12, 20, 0, 0, 'CET')),
],
[
'_id' => 'SF-06302000',
'performer' => 'The Rolling Stones',
'venue' => 'Soldier Field',
'genres' => [ 'rock', 'pop', 'blues' ],
'ticketsSold' => 59527,
'performanceDate' => new UTCDateTime(Carbon::create(2024, 6, 30, 20, 0, 0, 'CDT')),
],
];
Concert::insert($data);

$this->assertEquals(4, Concert::count());

$id = Concert::first()->id;

// begin model delete by id
$id = 'MSG-0212252000';
Concert::destroy($id);
// end model delete by id

$this->assertEquals(3, Concert::count());
$this->assertNull(Concert::find($id));
}

public function testModelDeleteModel(): void
{
require_once __DIR__ . '/Concert.php';
Concert::truncate();

$data = [
[
'performer' => 'Mitsuko Uchida',
'venue' => 'Carnegie Hall',
],
];
Concert::insert($data);

// begin delete one model
$concert = Concert::first();
$concert->delete();
// end delete one model

$this->assertEquals(0, Concert::count());
}

public function testModelDeleteFirst(): void
{
require_once __DIR__ . '/Concert.php';
Concert::truncate();

$data = [
[
'performer' => 'Mitsuko Uchida',
'venue' => 'Carnegie Hall',
],
[
'performer' => 'Brad Mehldau',
'venue' => 'Philharmonie de Paris',
],
[
'performer' => 'Billy Joel',
'venue' => 'Madison Square Garden',
],
[
'performer' => 'The Rolling Stones',
'venue' => 'Soldier Field',
],
];
Concert::insert($data);

// begin model delete one fluent
Concert::where('venue', 'Carnegie Hall')
->limit(1)
->delete();
// end model delete one fluent

$this->assertEquals(3, Concert::count());
}

public function testModelDeleteMultipleById(): void
{
require_once __DIR__ . '/Concert.php';
Concert::truncate();
$data = [
[
'_id' => 3,
'performer' => 'Mitsuko Uchida',
'venue' => 'Carnegie Hall',
],
[
'_id' => 5,
'performer' => 'Brad Mehldau',
'venue' => 'Philharmonie de Paris',
],
[
'_id' => 7,
'performer' => 'Billy Joel',
'venue' => 'Madison Square Garden',
],
[
'_id' => 9,
'performer' => 'The Rolling Stones',
'venue' => 'Soldier Field',
],
];
Concert::insert($data);

// begin model delete multiple by id
$ids = [3, 5, 7, 9];
Concert::destroy($ids);
// end model delete multiple by id

$this->assertEquals(0, Concert::count());
}

public function testModelDeleteMultiple(): void
{
require_once __DIR__ . '/Concert.php';
Concert::truncate();

$data = [
[
'performer' => 'Mitsuko Uchida',
'venue' => 'Carnegie Hall',
'genres' => ['classical'],
'ticketsSold' => 2121,
],
[
'performer' => 'Brad Mehldau',
'venue' => 'Philharmonie de Paris',
'genres' => [ 'jazz', 'post-bop' ],
'ticketsSold' => 5745,
],
[
'performer' => 'Billy Joel',
'venue' => 'Madison Square Garden',
'genres' => [ 'rock', 'soft rock', 'pop rock' ],
'ticketsSold' => 12852,
],
[
'performer' => 'The Rolling Stones',
'venue' => 'Soldier Field',
'genres' => [ 'rock', 'pop', 'blues' ],
'ticketsSold' => 59527,
],
];
Concert::insert($data);

// begin model delete multiple fluent
Concert::where('ticketsSold', '>', 7500)
->delete();
// end model delete multiple fluent

$this->assertEquals(2, Concert::count());
}
}