Question

I would like to dropDatabase in my mongo before my integration tests. Is it possible to do this through spring mongo ?

If it's not available for now, is it possible on fetching the com.mongodb.DB object somehow from spring mongo, so i can invoke it's dropDatabase() ?

Was it helpful?

Solution

A slightly cleaner solution which I am using is to use the MongoDbFactory object, as below:

mongoDbFactory.getDb().dropDatabase();

OTHER TIPS

Just in case someone needs the info, notice that since 3.0 there is no dropDatabase() method in the MongoDatabase api, so you have to write this instead:

mongoTemplate.getDb().drop();

>> source <<

Found it at last !

From mongodb shell :

> db.dropDatabase
function () {
    if (arguments.length) {
        throw "dropDatabase doesn't take arguments";
    }
    return this._dbCommand({dropDatabase:1});
}

combined with mongoOperations' executeCommand :

@Autowired private MongoOperations ops;

@BeforeMethod
public void dropDb() {
    this.ops.executeCommand("{dropDatabase:1}");
}

mongoTemplate.getDb().dropDatabase();

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top