[英]Differences between com.mongodb.client.MongoClient and com.mongodb.MongoClient
I'm a bit new in MongoDB and I got confused with MongoClient classes as there are two in different packages ( com.mongodb.client.MongoClient
and com.mongodb.MongoClient
).我对 MongoDB 有点陌生,我对 MongoClient 类感到困惑,因为在不同的包中有两个( com.mongodb.client.MongoClient
和com.mongodb.MongoClient
)。 And what I have seen is that they have more or less the same purpose but I could be wrong.我所看到的是他们或多或少有相同的目的,但我可能是错的。
What are the differences between both classes?两个班级之间有什么区别? Is one of them recommended over the other?是否推荐其中之一?
Use com.mongodb.client.MongoClient.create()
(as of 3.7 or later), legacy api com.mongodb.MongoClient()
is deprecated.使用com.mongodb.client.MongoClient.create()
(从 3.7 或更高版本开始),旧的 api com.mongodb.MongoClient()
已弃用。
A com.mongodb.client.MongoClient
instance represents a pool of connections to the database; com.mongodb.client.MongoClient
实例表示数据库连接池; you will only need one instance of class MongoClient even with multiple threads.即使有多个线程,您也只需要 MongoClient 类的一个实例。
Reference : http://mongodb.github.io/mongo-java-driver/3.12/driver/tutorials/connect-to-mongodb/参考: http : //mongodb.github.io/mongo-java-driver/3.12/driver/tutorials/connect-to-mongodb/
com.mongodb.client.MongoClient
is added since 3.7 release use below code to get instance with new API: com.mongodb.client.MongoClient
从 3.7 版本开始添加,使用以下代码获取具有新 API 的实例:
MongoClient mongoClient = MongoClients.create()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.