41

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?

qff
  • 5,524
  • 3
  • 37
  • 62

2 Answers2

70

PyMongo 2.4 up to at least 3.11.4

from pymongo import MongoClient
client = MongoClient('<HOST>', <PORT>)
client.drop_database('<DBNAME>')

PyMongo 2.3 and earlier

from pymongo import Connection
connection = Connection('<HOST>', <PORT>)
connection.drop_database('<DBNAME>')
qff
  • 5,524
  • 3
  • 37
  • 62
6
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/

zydcom
  • 490
  • 2
  • 8