-
Notifications
You must be signed in to change notification settings - Fork 1.5k
DOCSP-35976: Delete One usage example #2821
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
Changes from 4 commits
7092961
e15e104
bbab077
7ff62f8
cee53ff
eda988a
f3f2573
671993f
36bd315
2739d2f
c2f4d6e
f552540
c3213d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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' => 'Capote', | ||
'runtime' => 114, | ||
], | ||
]); | ||
|
||
// begin-delete-one | ||
$deleted = Movie::where('title', 'Capote') | ||
->orderBy('_id') | ||
->first() | ||
->delete(); | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
echo 'Deleted documents: ' . $deleted; | ||
// end-delete-one | ||
|
||
$this->assertTrue($deleted); | ||
$this->expectOutputString('Deleted documents: 1'); | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,66 @@ | ||||||||||||||||||||||||||||||||
.. _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 document and calling | ||||||||||||||||||||||||||||||||
the ``delete()`` method on an Eloquent model or a query builder. | ||||||||||||||||||||||||||||||||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
Pass a query filter to the ``where()`` method, sort the matching documents, and call the | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Is "query filter" defined somewhere in these docs yet? If so, it might be good to link to that somewhere. (ie: "For more information about query filters, see ... ") |
||||||||||||||||||||||||||||||||
``first()`` method to retrieve only the first document. Then, delete this matching document | ||||||||||||||||||||||||||||||||
by calling the ``delete()`` method. | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this paragraph could be more clear if it was presented as a list:
Suggested change
Although, looking at the update usage example page, it looks like that might not be inline with the formatting being used in these docs. An alternative suggestion would be:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Took your second suggestion! |
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
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. | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we should have a period on these since they're not full sentences (subject is in the intro)
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Feel free to double-check me on this one - I'm not 100% sure if we still consider it a fragment with the subject implied in the intro There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed - I think these are all fragments. Fixed! |
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
The example calls the following methods on the ``Movie`` model: | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
- ``where()``: matches documents in which the value of the ``title`` field is ``'Capote'``. | ||||||||||||||||||||||||||||||||
- ``orderBy()``: sorts matched documents by their ascending ``_id`` values. | ||||||||||||||||||||||||||||||||
- ``first()``: 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. | ||||||||||||||||||||||||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.