问题:
我想连接mongoDB并异步插入一些简单的数据,所以我使用了" mongodb-driver-async-3.0.2.jar ".但我发现我没有连接数据库.代码如下:
public static void main(String[] args) {
// connect to the local database server,default:127.0.0.1:27017
MongoClient mongoClient = MongoClients.create();
// get handle to "testDB" database
MongoDatabase database = (MongoDatabase) mongoClient.getDatabase("testDB");
SingleResultCallback<Void> callbackWhenFinished = new SingleResultCallback<Void>() {
@Override
public void onResult(final Void result, final Throwable t) {
System.out.println("Operation Finished!");
}
};
// get a handle to the "test" collection
MongoCollection<Document> collection = database.getCollection("test");
collection.insertOne(new Document("lala","hehe"),callbackWhenFinished);
}
Run Code Online (Sandbox Code Playgroud)
我确定我已经在127.0.0.1:27017启动了数据库服务,并且可以连接shell和非异步方法.错误:
PrimaryServerSelector从群集描述ClusterDescription中选择的服务器没有{type = UNKNOWN,connectionMode …