繁体   English   中英

MongoClient 上的实例化会产生错误

[英]Instantiation on MongoClient produces error

Despite all the documentation that says beyond MongoDB 3.7 the MongoClient class can be instantiated, my Eclipse IDE shouts that MongClient cannot be instantiated. 这里可能是什么问题?

Eclipse IDE 上的 MongoClient 实例化错误。

public class MongoDBExample 
{
    public static void main(String args[])
    {
        String result = null;
        System.out.println("Making a connection to MongoDB..!");
        MongoClient mongo_client = new MongoClient(); // ("mongodb://localhost:27017");
        result = mongo_client.getClass().toString();
        System.out.println("Result : " + result);
    }
}

您正在尝试使用 Legacy MongoDB MongoClient驱动程序 API 方式来初始化 MongoClient。

从 3.7 版开始,您应该这样做:

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;

public class MongoConnect {
        public static void main(String args[])
        {
            MongoClient mongo_client_constructor1 = MongoClients.create(); // ("mongodb://localhost:27017");
            MongoClient mongo_client_constructor2 = MongoClients.create("mongodb://hostOne:27017,hostTwo:27018");

        }
}

MongoDB Java Driver Legacy API 和 New ZDB974238714CA8DE634A7CE1DZF08A 之间的区别可以在这里清楚地找到

另请参阅MongoClients的 3.9 Javadoc 版本,这是 MongoClient 实例的工厂。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM