In PyMongo, you can delete a MongoDB collection using the drop()
method on the collection object. Here's how you can do it:
import pymongo
# Establish a connection to MongoDB
client = pymongo.MongoClient("mongodb://localhost:27017/")
# Select a database
db = client["your_database_name"]
# Select a collection
collection = db["your_collection_name"]
# Delete the collection
collection.drop()
print("Collection deleted successfully.")
Replace "mongodb://localhost:27017/"
with your MongoDB connection string, "your_database_name"
with the name of your database, and "your_collection_name"
with the name of the collection you want to delete.
Make sure you have the necessary permissions to delete collections in the specified database. Once executed, this code will delete the collection specified.
Top comments (0)