How To Delete Documents In MongoDB?

Rofl Facts
4 min readDec 4, 2023

--

The following techniques can be used in MongoDB to remove documents:

The inventory collection is used in the examples on this page. Create the inventory collection after establishing a connection to a test database in your MongoDB instance:

var documents = new[]
{
new BsonDocument
{
{ "item", "journal" },
{ "qty", 25 },
{ "size", new BsonDocument { { "h", 14 }, { "w", 21 }, { "uom", "cm" } } },
{ "status", "A" }
},
new BsonDocument
{
{ "item", "notebook" },
{ "qty", 50 },
{ "size", new BsonDocument { { "h", 8.5 }, { "w", 11 }, { "uom", "in" } } },
{ "status", "P" }
},
new BsonDocument
{
{ "item", "paper" },
{ "qty", 100 },
{ "size", new BsonDocument { { "h", 8.5 }, { "w", 11 }, { "uom", "in" } } },
{ "status", "D" }
},
new BsonDocument
{
{ "item", "planner" },
{ "qty", 75 },
{ "size", new BsonDocument { { "h", 22.85 }, { "w", 30 }, { "uom", "cm" } } },
{ "status", "D" }
},
new BsonDocument
{
{ "item", "postcard" },
{ "qty", 45 },
{ "size", new BsonDocument { { "h", 10 }, { "w", 15.25 }, { "uom", "cm" } } },
{ "status", "A" }
}
};
collection.InsertMany(documents);

Delete All Documents

Use an empty filter to remove all documents from a collection. constructors<BsonDocument>.Sort through.To the IMongoCollection, empty.the DeleteMany() function.

The example that follows removes every document from the inventory collection:

var filter = Builders<BsonDocument>.Filter.Empty;
var result = collection.DeleteMany(filter);

After it is executed successfully, the IMongoCollection.The number of documents that matched the filter is contained in the DeletedCount property of the instance of DeleteResult that the DeleteMany() function delivers.

Delete All Documents that Match a Condition

The documents to be deleted can be identified by filters, or criteria, that you set. The read operations and filters have the same syntax.

Use the Eq method to build a filter and provide equality conditions:

Builders<BsonDocument>.Filter.Eq(<field>, <value>);

To provide filter conditions, MongoDB offers a variety of query operators in addition to the equality filter. To generate a filter document, utilise the FilterDefinitionBuilder methods. For instance:

var builder = Builders<BsonDocument>.Filter;
builder.And(builder.Eq(<field1>, <value1>), builder.Lt(<field2>, <value2>));

Provide a filter option to the IMongoCollection in order to remove all documents that meet a deletion criterion the DeleteMany() function.

In the example that follows, every document in the inventory collection whose status field contains the letter “A” is eliminated:

var filter = Builders<BsonDocument>.Filter.Eq("status", "A");
var result = collection.DeleteMany(filter);

After it is executed successfully, the IMongoCollection the number of documents that matched the filter is contained in the DeletedCount property of the instance of DeleteResult that the DeleteMany() function delivers.

Delete Only One Document that Matches a Condition

Use the IMongoCollection to remove, at most, a single document that matches a given filter (even if the filter may match many documents).DeleteOne() technique.

The first document with a status of “D” is deleted in the example that follows:

var filter = Builders<BsonDocument>.Filter.Eq("status", "D");
var result = collection.DeleteOne(filter);

Delete a Document with MongoDB Atlas

Note: In the MongoDB Atlas UI, you can only remove one document at a time. Connect to your Atlas deployment using mongosh or a MongoDB driver in order to delete numerous documents. Then, follow the instructions on this page for your preferred approach.

This section’s example makes use of the sample movies dataset. See Load Sample Data for instructions on how to load the sample dataset into your MongoDB Atlas setup.

To remove a document from MongoDB Atlas, take the following actions:

1. Navigate to the collection.

a. In the sidebar of the MongoDB Atlas UI, select Database.

b. Click Browse Collections to view the database deployment containing the example data.

c. Click on the sample_mflix database in the left navigation window.

d. Choose the movie collection.

2. Specify a query filter document.

In the Filter area, you have the option to enter the query filter document. To define search parameters, a query filter document makes use of query operators.

Click Apply after pasting the query filter document into the Filter search bar:

{ genres: "Action", rated: { $in: [ "PG", "PG-13" ] } }

All entries in the sample_mflix.movies collection where the genres and ratings equal Action and PG or PG-13 are returned by this query filter.

3. Delete a document.

a. Hover your cursor over the document you wish to remove, then select the trash can icon that shows up on the right.

Upon selecting the “Delete” button, MongoDB Atlas marks the document for removal and requests your approval.

b. To verify your choice, click Delete.

Delete Behavior

Indexes

Even when removing every document from a collection, delete actions do not cause indexes to be dropped.

Atomicity

In MongoDB, every write action is atomic at the level of a single document. See Atomicity and Transactions for additional information about MongoDB and atomicity.

Write Acknowledgement

You can set the level of acknowledgement that MongoDB is asked to provide for write operations by using write concerns. See Write Concern for more information.

--

--