📜  MongoDB Drop Collection(1)

📅  最后修改于: 2023-12-03 15:17:41.611000             🧑  作者: Mango

MongoDB Drop Collection

Introduction

In MongoDB, a collection is a group of MongoDB documents. It is equivalent to a table in relational databases. Sometimes, we may want to delete a collection from the database. In this tutorial, we will learn how to use the MongoDB command to drop a collection.

Prerequisites

Before we begin, you should have:

  • Installed MongoDB on your machine
  • Connected to MongoDB Server
Syntax

The basic syntax of the drop() method in MongoDB is:

db.collection.drop()
Example

Suppose we have a database named sample_database with a collection named sample_collection. We can drop the sample_collection using the drop() method as follows:

use sample_database
db.sample_collection.drop()

If the sample_collection exists, it will be deleted from the database. And if it doesn't exist, MongoDB will return a message saying that the collection does not exist.

Conclusion

In this tutorial, we have learned how to use the drop() method in MongoDB to delete a collection. It is important to note that once a collection is dropped, the data in that collection is also deleted permanently. Therefore, make sure to take backups before deleting any collections.