小编赵学智*_*赵学智的帖子

无法从java连接到本地monogoDB

  1. 环境:mac os x 10.10
  2. MongoDB版本:3.0.5
  3. JDK版本:1.8
  4. MongoDB驱动程序:"mongo-java-driver-3.0.2.jar"和"mongodb-driver-async-3.0.2.jar"

问题:

我想连接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 …

java mongodb

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

java ×1

mongodb ×1