Docs Menu
Docs Home
/ / /
Laravel MongoDB
/

Delete a Document

On this page

  • Example

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.

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

  • Prints the number of deleted documents

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

$deleted = Movie::where('title', 'Quiz Show')
->orderBy('_id')
->limit(1)
->delete();
echo 'Deleted documents: ' . $deleted;

To learn how to edit your Laravel application to run the usage example, see the Usage Examples landing page.

Tip

To learn more about deleting documents with Laravel MongoDB, see the Deleting Models section of the Laravel documentation.

Back

Update Multiple Documents

Next

Delete Multiple Documents

On this page