我可以使用以下语句删除数据库:
MongoClient mongo = new MongoClient("localhost", 27017);
mongo.dropDatabase("d");
如何创建数据库?
最佳答案
如果您使用的是驱动程序 3.1.1 或更高版本:
引用这个answer :
Calling getDatabase doesn't in fact create new database because operation is lazy - it returns database representation. Calling any modifiable operation (e.g. createCollection):
will create new database for you if it is not present if present it will get database for you But remember that you have to call any operation which actually performs something - like create. If you just call getDatabase it won't create it.
注意
documentation不提供此信息,希望他们更新它。但长话短说,它只会在您对其调用操作后创建它。
关于java - 我如何从java创建mongodb数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45285368/