Updated March 6, 2023
Introduction to MongoDB Delete Collection
MongoDB delete collection has two methods available in MongoDB, after executing the drop command in MongoDB database server it will give the result in true or false format. If the collection is present in the database and it will delete successfully then it will return the result as true. If the collection is not present in the database and if it will not delete successfully then it will return the result as false. We can use the drop and remove method to delete any collection from MongoDB, also we need to give the collection name at the time of deleting the collection.
Syntax:
Below syntax, shows the delete of the collection in MongoDB.
1) Using drop method
db.collection_name.drop()
• In the above syntax collection name is defined as the name of collection which was we have used to delete from the database server.
• Drop method is used to drop the specified collection from the database server.
2) Using remove method
1) db.collection_name.remove ({})
2) db.collection_name.remove (<query>, {Writeconcern, justone})
In the above syntax, the collection name is defined as from which we have to remove the documents.
Remove is the method which was we have used to remove the documents from a collection.
If we pass the empty result set ({ }) remove method will delete all the documents from the collection.
The query parameter is defined as the remove specified documents from the collection, which criteria was we have provided in a query.
Just one parameter is defined as deletes only one document from the collection. We need to enable it by specifying the keyword as true. After using the true keyword in selection criteria only one document is deleted. If we have not used the true keyword in our selection criteria, then all the documents will be deleted from the collection.
• While using Writeconcern it will omit the default use of write concern.
How to delete the collection in MongoDB?
We can delete the collection in MongoDB by using two methods.
1) Drop
2) Remove
If we want to drop the whole collection and its indexes, then we can use the drop method to drop any collection from the database.
Using the drop method, the collection and their indexes will be deleted from the database server.
While using the remove method we can delete specified documents from the collection.
Using the remove method all the documents from the collection will be deleted but the indexes of the documents remain they will not be deleted from the database server.
After dropping any collection from the database server it will show below two resultsets.
1) True
2) False
The below example shows that resultset will show the true value at the time of dropping collection from the database server.
In the below example, the result set shows the output as true. The result set shows the result as true because the collection is removed from the database server.
> show collections
> db.test1.drop()
> show collections
The below example shows that resultset will show the false value at the time of dropping collection from the database server.
In the below example, the result set shows the output as false. The resultset shows the output as false because the collection is not removed from the database server or it is not present in the database.
> show collections
> db.test1.drop()
> show collections
For deleting any collection from the database, first, we need to connect to the particular database. Collection will not be deleted when we have not connected or used the specified database.
The below example shows that we need to use the specified database before deleting collection from the database.
> db.col_stat.drop()
> use test
> db.col_stat.drop()
> show collections
The first time we have not used a specified database at the time of deleting the collection, the same time it will show the false result, so the collection is not deleted from the database.
The second time we have used a specified database and then we have used the drop method, the same time collection is deleted and shows the resultset as true.
Example
The below example shows the delete collection in MongoDB.
1) Delete collection using drop method
In the below example, we have deleted the collection using the drop method. We have deleted the group by collection from the test database.
Code:
> show collections
> db.group_by.drop()
> show collections
2) Delete the specified documents from collection using remove method
In the below example, we have deleted specified documents from the collection using specified criteria.
We have deleted the document which has emp_code as 101. All the documents from emp_test collection are deleted which emp_code as 101.
Code:
> db.emp_test.find()
> db.emp_test.remove({emp_code:"101"})
> db.emp_test.find()
3) Delete single documents from collection using justone parameter
In the below example, we have deleted the single document from collection by using justone parameter.
We have used criteria on emp_code parameter that emp_code greater than 100. In the collection, we have seen that 3 documents which was emp_code greater than 100, but only one document is deleted while defining justone parameter value as true.
Code:
> db.emp_test.find()
> db.emp_test.remove({emp_code: {$gt: "100" } }, true );
> db.emp_test.find()
4) Delete all documents from collection using remove method
In the below example, we have deleted all the documents from the emp_test collection using the remove method.
We have not defined any select criteria using the remove command, so all the documents from the emp_test collection will be deleted.
We can see that in the below example all the documents from emp_test will be deleted but indexes and structure of the table are not deleted.
Code:
> db.emp_test.find()
> db.emp_test.remove({ })
> db.emp_test.find()
> show collections
Conclusion
Using the drop method, we can drop the collection and their indexes, and using the remove method we can delete a single or all the documents from the collection. If the collection is existing in the database, the resultset will show the result as true else it will show false in output.
Recommended Articles
This is a guide to MongoDB Delete Collection. Here we discuss the definition, How to delete the collection in MongoDB? along with examples respectively. You may also have a look at the following articles to learn more –