MongoDB

MongoDB Delete Collection Example

Hello readers, in this tutorial, we will see how to delete a collection in MongoDB. Let’s study in brief the different ways in which a MongoDB collection can be deleted.

1. Introduction

If you have installed the MongoDB application on Windows or Ubuntu operating system and you wish to learn about the delete operations then follow the below steps. It’s very simple to use these utilities, but before moving further let’s take a look at MongoDB.

1.1 What is MongoDB?

  • MongoDB is a high-performance NoSQL database where each database has collections which in turn has documents. Each document has a different number of fields, size, content, and is stored in a JSON-like format (i.e. Binary JSON (BSN)
  • The documents in MongoDB doesn’t need to have a schema defined beforehand. Instead, the fields (i.e. records) can be created on the go
  • Data model available within the MongoDB allows developers to represent the hierarchical relationships, store arrays, and other more complex structures easily
  • This NoSQL solution often comes with embedding, auto-sharding, and onboard replication for better scalability and high availability

2. MongoDB Delete Collection Example

In this tutorial, we will go through the different methods to drop/delete a collection in a Mongo database.

2.1 db.collection.remove()

In Mongo database, this method either deletes all documents from a collection or a single document that matches a specified condition. Here is what the syntax will look like.

Syntax

> db.collection_name.remove(Deletion_Criteria, <Optional_Parameter>)

where:

  • Deletion_Criteria is the condition on which the document has to be deleted
  • Optional_Parameter is a boolean value which when set to true or 1, deletes only a single document

Let’s understand this with the help of an example.

> db.inventory.remove({})

This command will drop all documents from the inventory collection as shown below.

Fig. 1: Remove all documents
Fig. 1: Remove all documents

Do note, if developers want to remove a single document that matches the given criteria, then developers can use the following syntax.

> db.inventory.remove( { status : "A" }, 1 )

The modified command accepts an optional parameter along with the deletion criteria and will remove a single document from the inventory collection.

Fig. 2: Remove single document
Fig. 2: Remove single document

2.2 db.collection.deleteOne()

This method was introduced in 3.2 version and drop at most a single document that matches the specified condition even though many documents may match the specified filter. Here is what the syntax will look like.

Syntax

> db.collection_name.deleteOne(Deletion_Criteria)

This method is similar to the db.inventory.remove() method with the optional parameter set to true or 1. Let’s understand this function with the help of an example.

> db.inventory.deleteOne( { status: "D" } )

This command will drop the first record that matches the specified condition.

Fig. 3: Delete first record
Fig. 3: Delete the first record

2.3 db.collection.deleteMany()

This method was again introduced in 3.2 version and deletes all documents that match the specified criteria. Here is what the syntax will look like.

Syntax

> db.collection_name.deleteMany(Deletion_Criteria)

Let’s understand this with the help of an example.

> db.inventory.deleteMany({})

This command will drop all documents from the inventory collection.

Fig. 4: Delete multiple documents
Fig. 4: Delete multiple documents

Do note, if developers want to remove all documents that match the specified criteria, they can use the following syntax.

> db.inventory.deleteMany({status : "A" })

Fig. 5: Delete selective documents
Fig. 5: Delete selective documents

That’s all for this post. Happy Learning!!

3. Conclusion

In this tutorial, we learned about the delete collection feature of the MongoDB. Developers can download the sample commands in the Downloads section.

4. Download the Eclipse Project

This was an example of a delete operation in the Mongo database.

Download
You can download the full source code of this example here: MongoDbDeleteCollectionEx

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button