Skip to content

DOCSP-35973: Delete Many usage example #2837

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
merged 4 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
42 changes: 42 additions & 0 deletions docs/includes/usage-examples/DeleteManyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers;

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

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

Movie::truncate();
Movie::insert([
[
'title' => 'Train Pulling into a Station',
'year' => 1896,
],
[
'title' => 'The Ball Game',
'year' => 1898,
],
]);

// begin-delete-many
$deleted = Movie::where('year', '<=', 1910)
->delete();

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

$this->assertEquals(2, $deleted);
$this->expectOutputString('Deleted documents: 2');
}
}
1 change: 1 addition & 0 deletions docs/usage-examples.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,4 @@ calls the controller function and returns the result to a web interface.
/usage-examples/findOne
/usage-examples/updateOne
/usage-examples/deleteOne
/usage-examples/deleteMany
67 changes: 67 additions & 0 deletions docs/usage-examples/deleteMany.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
.. _laravel-delete-many-usage:

=========================
Delete Multiple Documents
=========================

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

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

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

You can delete multiple documents in a collection by calling the ``delete()`` method on an
Eloquent model or a query builder.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:
The example currently shows it being chained to the results of the "where()" method rather than directly on the Eloquent model or by using the query builder DB facade.
I could be wrong, but I don't think it's a static method available on the model. I think it could be better to include a description of the way it's being called in the example, or to change the example to match one of the ways described.


To delete multiple documents, pass a query filter to the ``where()`` method. Then, delete the
matching documents 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 documents from the ``movies`` collection that match a query filter
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:
Similar to prior feedback, I think printing the output should be mentioned or the code that prints should be hidden.


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

- ``where()``: matches documents in which the value of the ``year`` field is less than or
equal to ``1910``
- ``delete()``: deletes the retrieved documents
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:

I think it could be helpful to add some information about the value printed even if it seems self-explanatory. E.g.
"The delete() method returns the number of documents successfully deleted"


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

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

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

Deleted documents: 7

To learn how to edit your Laravel application to run the usage example, see the
:ref:`Usage Examples 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:
I think linking to the Fundamentals > Write Operations page "Delete Documents" section could be more directly helpful. Since that content hasn't been merged yet, I would suggest creating a cleanup task in the cleanup ticket.


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