简体   繁体   中英

Java MongoDB: What is the difference between com.mongodb.DB and com.mongodb.client

I'm new using MongoDB, I'm working in a Java project and I started some tutorials to start working with the Driver.

I was using com.mongodb.client until I noticed that there was no findOne method in the com.mongodb.client.MongoCollection so I rewrited my project to use only com.mongodb.DB and the DBCollection library includes the findOne method which I need.

I was wondering what is the difference between those two libraries?

Thanks!

com.mongodb.DB is the old API for accessing Mongo before 3.x. You will find plenty of tutorials for those classes. The code is fully functional and you can use it for accessing Mongo 2.x and Mongo 3.x databases but it is not recommended to start a new project using it.

Since 3.0 the recommended way is via com.mongodb.MongoClient and com.mongodb.client.MongoDatabase . See the official tutorial of the Java driver here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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