mongo-db
  1. mongo-db-drop-collection

Drop Collection - (Database and Collection Operations)

In MongoDB, you can delete an entire collection using the drop() method. In this page, we will learn how to drop a collection in MongoDB.

Syntax

The drop() method requires the name of the collection you want to drop. Here is the syntax for dropping a collection in MongoDB:

db.collection_name.drop()

Example

Suppose we have a collection named "students". To drop this collection, run the following command in the MongoDB shell:

db.students.drop()

If the collection exists, it will be deleted successfully, and the following message will be displayed:

{ "ok" : 1 }

Output

When you drop a collection, all the documents in the collection are permanently deleted.

Explanation

The drop() method removes the entire collection, including all the documents in it. It is important to note that dropping a collection is a permanent operation and it cannot be undone. Once you drop a collection, all the documents in it will be lost forever.

Use

You can use the drop() method in many different scenarios. For example, if you have a test database where you keep sample data, you can drop the entire database once it is no longer required.

Important Points

  • The drop() method is used to delete a collection in MongoDB.
  • The command to drop a collection is db.collection_name.drop().
  • You cannot undo the drop operation, and it permanently deletes all documents in the collection.

Summary

In this page, we discussed how to delete a collection in MongoDB using the drop() method. We covered the syntax, example, output, explanation, use, and important points of dropping a collection. Dropping a collection is a permanent operation and should be used with caution to avoid accidentally deleting important data.

Published on: