Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I drop a MongoDB database using PyMongo?

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?

like image 420
qff Avatar asked Feb 17 '16 17:02

qff


People also ask

How do I drop a MongoDB database?

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.

Which method is used to drop a database in MongoDB?

MongoDB db. dropDatabase() command is used to drop a existing database.

How do I delete a collection in PyMongo?

To delete a MongoDB Collection, use db. collection. drop() command.


2 Answers

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

like image 158
qff Avatar answered Oct 09 '22 05:10

qff


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/

like image 41
zydcom Avatar answered Oct 09 '22 07:10

zydcom