Fastest way to reset MongoDB server / drop all databases
<p>I am starting a test MongoDB server using MongoBox and before each test method I am resetting it:</p>

<pre><code>class BaseTest(unittest.TestCase):

    def setUp(self):
        self.purge_database()

    @cached_property
    def mongo_client(self):
        return pymongo.mongo_client.MongoClient(host=settings.DEFAULT_DB)

    def purge_database(self, drop=True):
        for db_name in self.mongo_client.database_names():
            db = getattr(self.mongo_client, db_name)
            db.eval("""
                db.getCollectionNames().forEach( function(collection_name) {
                    if (collection_name.indexOf("system.") == -1)
                        db[collection_name].drop();
                    else
                        db.collection_name.remove({});
                });
            """)
</code></pre>

<p>My tests run too slow :</p>

<pre><code>Ran 311 tests in 685.171s
</code></pre>

<p>This variant</p>

<pre><code>def purge_database(self, drop=True):
    for db_name in self.mongo_client.database_names():
        self.mongo_client.drop_database(db_name)
</code></pre>

<p>is slow too</p>

<pre><code>Ran 311 tests in 678.837s
</code></pre>

<p>And this</p>

<pre><code>    # exclude system databases
    database_names = (
        db_name for db_name in
        self.mongo_client.database_names()
        if db_name not in ['local']
    )

    for db_name in database_names:
        db = self.mongo_client[db_name]

        # exclude system collections
        collections = (
            db[c] for c in db.collection_names()
            if not c.startswith('system.')
        )
        for collection in collections:
            if drop:
                db.drop_collection(collection)
            else:
                try:
                    collection.remove(None)
                except pymongo.errors.OperationFailure:
                    if collection.options().get('capped', False):
                        # cannot remove documents from capped collections
                        # in latest version of Mongo. Dropping instead.
                        db.drop_collection(collection)
</code></pre>

<p>too:</p>

<pre><code>Ran 311 tests in 657.224s
</code></pre>

<p>Do you know any other methods to quickly clear the database for each test method?</p>