java - 我如何从java创建mongodb数据库

标签 java mongodb

我可以使用以下语句删除数据库:

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/

相关文章:

java - 有没有办法获取带有结果集的 JPA 命名查询的计数大小?

node.js - Mongoose 在哪里查询 "or"

javascript - 计算数组中相同值的个数

java - 使用 Spring 3.2 和 Mockito 进行 Controller 单元测试的上下文初始化问题

java - 构造函数调用数组创建

node.js - Mongodb更新$pull总是返回1

mongodb - 为什么匹配聚合查询中的日期被忽略?

c# - 在同一节点上构建过滤器

java - java中的信号量阻塞

java - Google Firebase HTTP 发布