Clear mongodb using spring-mongodb
mongoTemplate.getDb().dropDatabase();
can help if you will not use indexes.
In case of using indexes through spring-data-mongodb, for example @Index
annotation, dropDatabase()
will drop indexes as well.
My solution is cleaning up documents:
private void cleanUp() { for (String collectionName : mongoTemplate.getCollectionNames()) { if (!collectionName.startsWith("system.")) { mongoTemplate.getCollection(collectionName).remove(new BasicDBObject()); } }}