-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
DOCSP-35951: write operations delete docs #2829
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a few small things!
- :ref:`Pruning <laravel-model-pruning>`, which lets you define conditions | ||
in which a document should be deleted automatically |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- :ref:`Pruning <laravel-model-pruning>`, which lets you define conditions | |
in which a document should be deleted automatically | |
- :ref:`Pruning <laravel-model-pruning>`, which lets you define conditions | |
that mark documents for automatic deletion |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing out the unclear wording. Since the conditions don't mark documents directly, so maybe I'll use "define conditions that qualify a document for automatic deletion".
- Call the ``destroy()`` method on the model to delete a document by the | ||
primary key. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Call the ``destroy()`` method on the model to delete a document by the | |
primary key. | |
- Call the ``destroy()`` method on the model to delete a document by matching the | |
primary key. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the existing sentence and suggestion might be omitting a detail necessary to understand what to pass it, so I'll figure out how to reword. Will also update the description in the corresponding section for deleting multiple docs.
The following example shows how to delete a document by calling ``delete()`` | ||
on an instance of the model: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
S: separate the three examples into subsections or move into bullet points, but as of now stacking them is a bit confusing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with this and had considered earlier on, but wanted to keep all examples at the same heading level. Both Insert and Update use the same formatting with the same intention.
I think once these sections (Insert, Modify, Delete) are split into individual pages similar to drivers docs are separate pages, these can be their own sections.
In the meantime, I'll add literalinclude captions (including examples in the prior sections) to identify which method is being demonstrated.
Delete Multiple Documents Examples | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
You can perform deletes on multiple documents in the following ways: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can perform deletes on multiple documents in the following ways: | |
You can delete multiple documents in the following ways: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm with a few last comments!
- Call the ``destroy()`` method on the model and pass it a primary key to | ||
delete the matching document. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
S: revise to avoid the phrasing "pass it"
Ie
- Call the ``destroy()`` method on the model and pass it a primary key to | |
delete the matching document. | |
- Call the ``destroy()`` method on the model while passing a primary key to | |
delete the matching document. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussed on Slack. Replacing with a adverbial phrase to describe the first part of the sentence.
- Call the ``destroy()`` method on the model, passing it the primary key of | ||
the document to be deleted. | ||
- Call the ``delete()`` method on an instance of the model. | ||
- Chain methods to retrieve and delete an instance of a model by calling the | ||
``delete()`` method. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$model->delete()
should be the first in the list as it's probably the more used.
The alternate Model::destroy($ids)
accepts a single id, a list of ids, or a list of model intances.
- Call the ``destroy()`` method on the model, passing it the primary key of | |
the document to be deleted. | |
- Call the ``delete()`` method on an instance of the model. | |
- Chain methods to retrieve and delete an instance of a model by calling the | |
``delete()`` method. | |
- Call the ``$model->delete()`` method on an instance of the model. | |
- Call the ``Model::destroy($ids)`` method on the model class, 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question:
Is the "$model->delete()" a common way to represent a placeholder for an Eloquent model for a Laravel developer audience?
Otherwise, what would be a common way to explain calling on a method on an instance of a model? I could be wrong, but I suspect the current suggestion might be repetitive (the instance and method are shown, but it's still described as a method).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that code is more explicit than words. This is the convention I use systematically on this project when I want to emphasize the scope of a method.
I've found examples of Model::method
in Eloquent doc, but not $model->method
. See https://laravel.com/docs/11.x/eloquent#deleting-models
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with changing conventions, but is this something we could come back to and decide later? I think it might involve updating several of the currently published docs to make them use it consistently. If so, I can ticket that.
|
||
// begin model delete one fluent | ||
Concert::where('venue', 'Carnegie Hall') | ||
->orderBy('_id') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't get the point of ordering for deleting. In fact, it's ignored:
laravel-mongodb/src/Query/Builder.php
Lines 787 to 814 in 95c5b4f
public function delete($id = null) | |
{ | |
// If an ID is passed to the method, we will set the where clause to check | |
// the ID to allow developers to simply and quickly remove a single row | |
// from their database without manually specifying the where clauses. | |
if ($id !== null) { | |
$this->where('_id', '=', $id); | |
} | |
$wheres = $this->compileWheres(); | |
$options = $this->inheritConnectionOptions(); | |
if (is_int($this->limit)) { | |
if ($this->limit !== 1) { | |
throw new LogicException(sprintf('Delete limit can be 1 or null (unlimited). Got %d', $this->limit)); | |
} | |
$result = $this->collection->deleteOne($wheres, $options); | |
} else { | |
$result = $this->collection->deleteMany($wheres, $options); | |
} | |
if ($result->isAcknowledged()) { | |
return $result->getDeletedCount(); | |
} | |
return 0; | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, so if the where() query filter matched multiple documents, wouldn't it be important to specify the sort order such that it's known which one will surface when calling limit(1), since that's the one that will be deleted? Does that mean that you can't use indexes with sort orders for the query filter part of the delete operation?
If not possible, I should probably include that information.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's currently not possible. That could be a new feature PHPORM-169
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does that mean that you can't use indexes with sort orders for the query filter part of the delete operation?
Note: from the server's perspective, there is no way to specify an order with a delete operation.
Indexes can certainly be used (and hinted). In the case of a sparse index that might actually have an impact on which document(s) get selected for the deletion.
docs/includes/fundamentals/write-operations/WriteOperationsTest.php
Outdated
Show resolved
Hide resolved
docs/includes/fundamentals/write-operations/WriteOperationsTest.php
Outdated
Show resolved
Hide resolved
|
||
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 permanently. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"permanently" is wrong when the SoftDeletes
trait is used. But you mention it just below, so maybe that's okay.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for providing that perspective, I'll update the text to make that more clear!
JIRA:
https://jira.mongodb.org/browse/DOCSP-35951
Staging:
https://preview-mongodbcchomongodb.gatsbyjs.io/laravel/DOCSP-35951-write-operations-delete/fundamentals/write-operations/#delete-documents
Note:
This PR is stacked with all the write operations PRs and is the final section.
Relevant changes:
Checklist