Introduction
In this article, we will learn how to drop a collection in MongoDB in Python. MongoDB is a NoSQL database used for storing high-volumes of data. Dropping a collection in MongoDB means removing it along with all its data.
Prerequisites
Before we get started, you should have the following:
- Python installed on your computer
- Pymongo library installed
- MongoDB installed on your computer
- A collection in your MongoDB database
Method 1: Using drop() method
The easiest way to drop a MongoDB collection is by using the drop()
method. The drop()
method is a built-in method in MongoDB and can be used to drop a collection.
Here’s how you can use the drop()
method to drop a collection:
import pymongo # Connect to MongoDB client = pymongo.MongoClient("mongodb://localhost:27017/") db = client["mydatabase"] # Drop collection named "customers" db["customers"].drop()
Here, we first import the pymongo
library and then connect to the MongoDB server running on localhost
on the default port of 27017
.
Next, we create a variable called db
that points to the database we want to work with. We have assumed that the name of the database is mydatabase
.
Finally, we call the drop()
method on the customers
collection to drop it.
Output:
{'nIndexesWas': 1, 'ns': 'mydatabase.customers', 'ok': 1.0}
The output shows that the operation was successful.
Method 2: Using drop_collection() method
Another way to drop a MongoDB collection is by using the drop_collection()
method. The drop_collection()
method is a method of the Collection
class in pymongo.
Here’s how you can use the drop_collection()
method to drop a collection:
import pymongo # Connect to MongoDB client = pymongo.MongoClient("mongodb://localhost:27017/") db = client["mydatabase"] # Get the "customers" collection collection = db["customers"] # Drop the collection collection.drop()
Here, we first import the pymongo
library and then connect to the MongoDB server running on localhost
on the default port of 27017
.
Next, we create a variable called db
that points to the database we want to work with. We have assumed that the name of the database is mydatabase
.
Then, we create a variable called collection
that points to the customers
collection. Finally, we call the drop()
method on the collection
variable to drop it.
Output:
{'nIndexesWas': 1, 'ns': 'mydatabase.customers', 'ok': 1.0}
The output shows that the operation was successful.
Method 3: Using command() method
MongoDB also provides another way to drop a collection by using the command()
method. The command()
method is a method of the Database
class in pymongo.
Here’s how you can use the command()
method to drop a collection:
import pymongo # Connect to MongoDB client = pymongo.MongoClient("mongodb://localhost:27017/") db = client["mydatabase"] # Drop collection using command function db.command("drop", "customers")
Here, we first import the pymongo
library and then connect to the MongoDB server running on localhost
on the default port of 27017
.
Next, we create a variable called db
that points to the database we want to work with. We have assumed that the name of the database is mydatabase
.
Finally, we call the command()
method on the db
variable with the command drop
and the name of the collection to drop.
Output:
{'ok': 1.0}
The output shows that the operation was successful.
Conclusion
In this article, we learned how to drop a MongoDB collection using PyMongo in Python. We used three different methods to drop a collection, including the drop()
, drop_collection()
, and command()
methods. All of these methods can be used to drop collections and they all have their advantages depending on the situation.