Hướng dẫn mongodb delete by filter

Docs HomeMongoDB Manual

On this page

  • Delete All Documents
  • Delete All Documents that Match a Condition
  • Delete Only One Document that Matches a Condition
  • Delete Behavior


➤ Use the Select your language drop-down menu in the upper-right to set the language of the following examples.


Delete operations do not drop indexes, even if deleting all documents from a collection.

All write operations in MongoDB are atomic on the level of a single document. For more information on MongoDB and atomicity, see Atomicity and Transactions.

With write concerns, you can specify the level of acknowledgement requested from MongoDB for write operations. For details, see Write Concern.

Docs HomeMongoDB Manual

CRUD operations create, read, update, and delete documents.

Create or insert operations add new documents to a collection. If the collection does not currently exist, insert operations will create the collection.

MongoDB provides the following methods to insert documents into a collection:

  • db.collection.insertOne() New in version 3.2

  • db.collection.insertMany() New in version 3.2

In MongoDB, insert operations target a single collection. All write operations in MongoDB are atomic on the level of a single document.

For examples, see Insert Documents.

Read operations retrieve documents from a collection; i.e. query a collection for documents. MongoDB provides the following methods to read documents from a collection:

  • db.collection.find()

You can specify query filters or criteria that identify the documents to return.

Hướng dẫn mongodb delete by filter

For examples, see:

  • Query Documents

  • Query on Embedded/Nested Documents

  • Query an Array

  • Query an Array of Embedded Documents

Update operations modify existing documents in a collection. MongoDB provides the following methods to update documents of a collection:

  • db.collection.updateOne() New in version 3.2

  • db.collection.updateMany() New in version 3.2

  • db.collection.replaceOne() New in version 3.2

In MongoDB, update operations target a single collection. All write operations in MongoDB are atomic on the level of a single document.

You can specify criteria, or filters, that identify the documents to update. These filters use the same syntax as read operations.

For examples, see Update Documents.

Delete operations remove documents from a collection. MongoDB provides the following methods to delete documents of a collection:

  • db.collection.deleteOne() New in version 3.2

  • db.collection.deleteMany() New in version 3.2

In MongoDB, delete operations target a single collection. All write operations in MongoDB are atomic on the level of a single document.

You can specify criteria, or filters, that identify the documents to remove. These filters use the same syntax as read operations.

For examples, see Delete Documents.

MongoDB provides the ability to perform write operations in bulk. For details, see Bulk Write Operations.

Docs HomeStart with Guides

In this guide, you will delete data from MongoDB.

Time required: 10 minutes

  • A connection string to your MongoDB deployment.

  • Sample datasets loaded into your cluster.

  • An installed MongoDB Driver.

  • Data inserted from the Insert Data into MongoDB guide.

Switch to the database and collection you wish to query. In this case you will be using the sample_guides database and comets collection.

The following example illustrates using a query filter to delete documents where their orbitalPeriod is greater than 5 and less than 85.

The result contains some information about the delete operation. To verify you deleted documents, print the amount of documents the driver deleted.

Here is the complete code followed by sample output.

If you have completed this guide, you have deleted data from MongoDB.

That completes this introduction to CRUD operations in MongoDB.

See the following resources for more in-depth information about the concepts presented here:

  • Delete Documents

  • Delete Methods