MongoDB – Drop Collection

MongoDB Delete Collection – In this MongoDB Tutorial, we shall learn to delete or drop a MongoDB Collection.

To delete a MongoDB Collection, use db.collection.drop() command. Following is a step by step guide :

Step 1: Select the database where your collection is, with USE command.

use

Step 2: Verify if the collection is present.

show collections

Step 3: Issue drop() command on the collection. Syntax of drop() command is provided below :

db..drop()

where is mandatory and is the name of MongoDB Collection to be deleted.

Step 4: If the Collection is deleted successfully then ‘true‘ is echoed back as acknowledgement, else ‘false’ would be echoed back. 

MongoDB Drop Collection Example 

The following example is for a database gfg. The collections are student_gfg, teacher, and semester.

Begin by checking the available collections in your database. Here the database is gfg.  

[> use gfg  switched to db gfg [> show collections  student_gfg  teacher  semester  >

Now for dropping the teacher collection from the database gfg, we will use the drop collection method:

db.teacher.drop()

The method will drop the teacher collection and its documents. The result will be as follows:

[> db.teacher.drop()  true [> show collections  student_gfg  semester  >

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *