Bertie
Bertie

Reputation: 17697

dropDatabase from spring mongo

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() ?

Upvotes: 3

Views: 2800

Answers (4)

onofricamila
onofricamila

Reputation: 1089

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 <<

Upvotes: 5

GSK
GSK

Reputation: 563

mongoTemplate.getDb().dropDatabase();

Upvotes: 1

Trevor Gowing
Trevor Gowing

Reputation: 2296

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

mongoDbFactory.getDb().dropDatabase();

Upvotes: 3

Bertie
Bertie

Reputation: 17697

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

Upvotes: 1

Related Questions