I want to drop a database in MongoDB similarly to
use <DBNAME> db.dropDatabase()
in the Mongo shell.
How do I do that in PyMongo?
The best way to do it is from the mongodb console: > use mydb; > db. dropDatabase(); Alternatively, you can stop mongod and delete the data files from your data directory, then restart.
MongoDB db. dropDatabase() command is used to drop a existing database.
To delete a MongoDB Collection, use db. collection. drop() command.
PyMongo 2.4 up to at least 3.11.4
from pymongo import MongoClient client = MongoClient('<HOST>', <PORT>) client.drop_database('<DBNAME>')
PyMongo Stable documentation
PyMongo 3.2.1 documentation
PyMongo 2.3 and earlier
from pymongo import Connection connection = Connection('<HOST>', <PORT>) connection.drop_database('<DBNAME>')
PyMongo 2.3 documentation
PyMongo 1.0 documentation
from pymongo import MongoClient client = MongoClient('<HOST>', <PORT>) client.db.command("dropDatabase")
see copydb example: https://api.mongodb.org/python/current/examples/copydb.html
You can also use runCommand helper to run other commands, detail see https://docs.mongodb.org/v3.0/reference/command/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With