在集成测试之前,我想在我的mongo中删除数据库。是否有可能通过春天mongo这样做?
如果它现在不可用,是否可以以某种方式从spring mongo获取com.mongodb.DB对象,所以我可以调用它的dropDatabase()?
我使用的一个稍微清洁的解决方案是使用MongoDbFactory对象,如下所示:
mongoDbFactory.getDb().dropDatabase();
为了防止有人需要这些信息,请注意,自3.0以来MongoDatabase api中没有dropDatabase()方法,所以你必须改为:
mongoTemplate.getDb().drop();
终于找到了!
来自mongodb shell:
> db.dropDatabase
function () {
if (arguments.length) {
throw "dropDatabase doesn't take arguments";
}
return this._dbCommand({dropDatabase:1});
}
与mongoOperations' executeCommand结合:
@Autowired private MongoOperations ops;
@BeforeMethod
public void dropDb() {
this.ops.executeCommand("{dropDatabase:1}");
}
mongoTemplate.getDb()dropDatabase();