Dropping all collections in Mongoengine

DantheMan picture DantheMan · Apr 8, 2013 · Viewed 8.1k times · Source

I have searched the api, but can't find anything relating to the dropping of a database without iterating through the collections manually.

Is there a simpler way of calling db.dropDatabase() through mongoengine? Its not a big deal to iterate through just wanted a simpler way.

Answer

alecxe picture alecxe · Apr 8, 2013

How about doing it this way?

from mongoengine import connect

db = connect('test')
db.drop_database('test')

Alternatively, you can get connection object from _get_db() method:

from mongoengine import connect
from mongoengine.connection import _get_db

connect('test')

db = _get_db()
db.connection.drop_database('test')