Docs Menu
Docs Home
/ / /
Laravel MongoDB
/

Delete Multiple Documents

On this page

  • Example

You can delete multiple documents in a collection by calling the delete() method on an object collection or a query builder.

To delete multiple documents, pass a query filter to the where() method. Then, delete the matching documents 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 documents from the movies collection that match 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 year field is less than or equal to 1910.

  • delete(): deletes the matched documents. This method returns the number of documents that the method successfully deletes.

$deleted = Movie::where('year', '<=', 1910)
->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 Delete Documents section of the Write Operations guide.

Back

Delete a Document

Next

Count Documents

On this page