Home » Software Development » MongoDB » MongoDB Delete Collection Example

About Yatin

Yatin
The author is graduated in Electronics & Telecommunication. During his studies, he has been involved with a significant number of projects ranging from programming and software engineering to telecommunications analysis. He works as a technical lead in the information technology sector where he is primarily involved with projects based on Java/J2EE technologies platform and novel UI technologies.

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
(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of