Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropping all collections in Mongoengine

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.

like image 263
DantheMan Avatar asked Apr 08 '13 18:04

DantheMan


People also ask

How do I drop all collections in MongoDB?

In MongoDB, db. collection. drop() method is used to drop a collection from a database. It completely removes a collection from the database and does not leave any indexes associated with the dropped collections.

Does removing all collections in a database also remove the database?

Yes, to all.

Which function will remove collection in MongoDB?

The remove() Method MongoDB's remove() method is used to remove a document from the collection. remove() method accepts two parameters. One is deletion criteria and second is justOne flag. deletion criteria − (Optional) deletion criteria according to documents will be removed.

How do I drop a collection in MongoDB Atlas?

The drop command removes the specified collection or view from the federated database instance storage configuration. Use the wildcard "*" to remove all collections generated by the wildcard collection function (that is, collectionName() ), including the wildcard collection rule itself.


1 Answers

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')
like image 110
alecxe Avatar answered Oct 13 '22 19:10

alecxe