user2465233
user2465233

Reputation: 129

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!

Upvotes: 4

Views: 4468

Answers (1)

Robert
Robert

Reputation: 42829

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.

Upvotes: 7

Related Questions