Skip to content

Commit d2de40a

Browse files
norareidyChris Cho
authored and
Chris Cho
committed
DOCSP-35976: Delete One usage example (mongodb#2821)
Adds a usage example page demonstrating how to delete one document from a collection --------- Co-authored-by: norareidy <norareidy@users.noreply.github.com>
1 parent ede97da commit d2de40a

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Http\Controllers;
6+
7+
use App\Models\Movie;
8+
use MongoDB\Laravel\Tests\TestCase;
9+
10+
class DeleteOneTest extends TestCase
11+
{
12+
/**
13+
* @runInSeparateProcess
14+
* @preserveGlobalState disabled
15+
*/
16+
public function testDeleteOne(): void
17+
{
18+
require_once __DIR__ . '/Movie.php';
19+
20+
Movie::truncate();
21+
Movie::insert([
22+
[
23+
'title' => 'Quiz Show',
24+
'runtime' => 133,
25+
],
26+
]);
27+
28+
// begin-delete-one
29+
$deleted = Movie::where('title', 'Quiz Show')
30+
->orderBy('_id')
31+
->limit(1)
32+
->delete();
33+
34+
echo 'Deleted documents: ' . $deleted;
35+
// end-delete-one
36+
37+
$this->assertEquals(1, $deleted);
38+
$this->expectOutputString('Deleted documents: 1');
39+
}
40+
}

docs/usage-examples.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,4 @@ calls the controller function and returns the result to a web interface.
7373

7474
/usage-examples/findOne
7575
/usage-examples/updateOne
76+
/usage-examples/deleteOne

docs/usage-examples/deleteOne.txt

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
.. _laravel-delete-one-usage:
2+
3+
=================
4+
Delete a Document
5+
=================
6+
7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. meta::
12+
:keywords: delete one, remove, code example
13+
14+
.. contents:: On this page
15+
:local:
16+
:backlinks: none
17+
:depth: 1
18+
:class: singlecol
19+
20+
You can delete a document in a collection by retrieving a single Eloquent model and calling
21+
the ``delete()`` method, or by calling ``delete()`` directly on a query builder.
22+
23+
To delete a document, pass a query filter to the ``where()`` method, sort the matching documents,
24+
and call the ``limit()`` method to retrieve only the first document. Then, delete this matching
25+
document by calling the ``delete()`` method.
26+
27+
Example
28+
-------
29+
30+
This usage example performs the following actions:
31+
32+
- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the
33+
``sample_mflix`` database
34+
- Deletes a document from the ``movies`` collection that matches a query filter
35+
36+
The example calls the following methods on the ``Movie`` model:
37+
38+
- ``where()``: matches documents in which the value of the ``title`` field is ``'Quiz Show'``
39+
- ``orderBy()``: sorts matched documents by their ascending ``_id`` values
40+
- ``limit()``: retrieves only the first matching document
41+
- ``delete()``: deletes the retrieved document
42+
43+
.. io-code-block::
44+
:copyable: true
45+
46+
.. input:: ../includes/usage-examples/DeleteOneTest.php
47+
:start-after: begin-delete-one
48+
:end-before: end-delete-one
49+
:language: php
50+
:dedent:
51+
52+
.. output::
53+
:language: console
54+
:visible: false
55+
56+
Deleted documents: 1
57+
58+
For instructions on editing your Laravel application to run the usage example, see the
59+
:ref:`Usage Example landing page <laravel-usage-examples>`.
60+
61+
.. tip::
62+
63+
To learn more about deleting documents with {+odm-short+}, see the `Deleting Models
64+
<https://laravel.com/docs/{+laravel-docs-version+}/eloquent#deleting-models>`__ section of the
65+
Laravel documentation.
66+
67+
For more information about query filters, see the :ref:`laravel-retrieve-matching` section of
68+
the Read Operations guide.
69+

0 commit comments

Comments
 (0)