Skip to content

Commit

Permalink
DOCSP-35976: Delete One usage example (#2821)
Browse files Browse the repository at this point in the history
Adds a usage example page demonstrating how to delete one document from a collection
---------

Co-authored-by: norareidy <[email protected]>
  • Loading branch information
norareidy and norareidy authored Apr 8, 2024
1 parent 2b0b5e6 commit 10f44bf
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
40 changes: 40 additions & 0 deletions docs/includes/usage-examples/DeleteOneTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers;

use App\Models\Movie;
use MongoDB\Laravel\Tests\TestCase;

class DeleteOneTest extends TestCase
{
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testDeleteOne(): void
{
require_once __DIR__ . '/Movie.php';

Movie::truncate();
Movie::insert([
[
'title' => 'Quiz Show',
'runtime' => 133,
],
]);

// begin-delete-one
$deleted = Movie::where('title', 'Quiz Show')
->orderBy('_id')
->limit(1)
->delete();

echo 'Deleted documents: ' . $deleted;
// end-delete-one

$this->assertEquals(1, $deleted);
$this->expectOutputString('Deleted documents: 1');
}
}
1 change: 1 addition & 0 deletions docs/usage-examples.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,4 @@ calls the controller function and returns the result to a web interface.

/usage-examples/findOne
/usage-examples/updateOne
/usage-examples/deleteOne
69 changes: 69 additions & 0 deletions docs/usage-examples/deleteOne.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
.. _laravel-delete-one-usage:

=================
Delete a Document
=================

.. facet::
:name: genre
:values: reference

.. meta::
:keywords: delete one, remove, code example

.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol

You can delete a document in a collection by retrieving a single Eloquent model and calling
the ``delete()`` method, or by calling ``delete()`` directly on a query builder.

To delete a document, pass a query filter to the ``where()`` method, sort the matching documents,
and call the ``limit()`` method to retrieve only the first document. Then, delete this matching
document by calling the ``delete()`` method.

Example
-------

This usage example performs the following actions:

- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the
``sample_mflix`` database
- Deletes a document from the ``movies`` collection that matches a query filter

The example calls the following methods on the ``Movie`` model:

- ``where()``: matches documents in which the value of the ``title`` field is ``'Quiz Show'``
- ``orderBy()``: sorts matched documents by their ascending ``_id`` values
- ``limit()``: retrieves only the first matching document
- ``delete()``: deletes the retrieved document

.. io-code-block::
:copyable: true

.. input:: ../includes/usage-examples/DeleteOneTest.php
:start-after: begin-delete-one
:end-before: end-delete-one
:language: php
:dedent:

.. output::
:language: console
:visible: false

Deleted documents: 1

For instructions on editing your Laravel application to run the usage example, see the
:ref:`Usage Example landing page <laravel-usage-examples>`.

.. tip::

To learn more about deleting documents with {+odm-short+}, see the `Deleting Models
<https://laravel.com/docs/{+laravel-docs-version+}/eloquent#deleting-models>`__ section of the
Laravel documentation.

For more information about query filters, see the :ref:`laravel-retrieve-matching` section of
the Read Operations guide.

0 comments on commit 10f44bf

Please sign in to comment.