Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

mongodb

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!

like image 642
user2465233 Avatar asked Mar 12 '23 12:03

user2465233


1 Answers

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.

like image 52
Robert Avatar answered Mar 15 '23 16:03

Robert