Clear mongodb using spring-mongodb Clear mongodb using spring-mongodb mongodb mongodb

Clear mongodb using spring-mongodb


Try this:

@Autowiredorg.springframework.data.mongodb.core.MongoTemplate mongoTemplate;.....mongoTemplate.getDb().dropDatabase();


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());            }        }}


this is the non-deprecated answer:mongoTemplate.getDb().drop();