Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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). 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?

like image 521
PhoneixS Avatar asked Feb 17 '20 16:02

PhoneixS


People also ask

What is MongoDB MongoClient?

The MongoClient class is a class that allows for making Connections to MongoDB. remarks. The programmatically provided options take precedence over the URI options. example // Connect using a MongoClient instance const MongoClient = require('mongodb').

How does MongoDB connect to MongoClient?

Connect to a Replica Setconst MongoClient = require('mongodb'). MongoClient; const assert = require('assert'); // Connection URL const url = 'mongodb://localhost:27017,localhost:27018/?replicaSet=foo'; // Database Name const dbName = 'myproject'; // Use connect method to connect to the Server MongoClient.

What is MongoClient in Java?

MongoClient is the interface between our java program and MongoDB server. MongoClient is used to create connection, connect to database, retrieve collection names and create/read/update/delete database, collections, document etc.

Is MongoClient a singleton?

Typically yes, it is a good practice to make MongoClient a singleton. As mentioned in the MongoDB Java driver: The MongoClient instance represents a pool of connections to the database; you will only need one instance of class MongoClient even with multiple threads.

Which MongoDB client should I use?

That indicates com.mongodb.client.MongoClient is the one you can consider using. I guess it depends upon the version of the driver software you are using. Prior to driver version 3.7 there is only one option: com.mongodb.MongoClient.

What is the difference between NodeJS-MongoDB and mongoose?

These two are different packages through which you can connect to MongoDB using node.js. If you use mongoose then probably you might not need nodejs-mongodb native driver as mongoose has a lot of functions which are basically wrappers to existing native driver.

How do I create a mongoclient object in Java?

From the MongoDB Java Driver 3.12 (reference) documentation, com.mongodb.client.MongoClient mongoClient = MongoClients.create (); is the way to create a MongoClient object, and the legacy way is the com.mongodb.MongoClient mongoClient = new MongoClient ();. That indicates com.mongodb.client.MongoClient is the one you can consider using.

Is it possible to use mongoose as a database driver?

Internally mongoose would use native driver. If you wanted to make your MongoDB look schema based then mongoose can help you a lot as you'll define schema for your collection & operate on those schemas while data insertion. Thanks for contributing an answer to Stack Overflow!


1 Answers

Use com.mongodb.client.MongoClient.create() (as of 3.7 or later), legacy api com.mongodb.MongoClient() is deprecated.

A com.mongodb.client.MongoClient instance represents a pool of connections to the database; you will only need one instance of class MongoClient even with multiple threads.

Reference : 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:

    MongoClient mongoClient = MongoClients.create()
like image 175
Umeshsolanki Avatar answered Sep 30 '22 18:09

Umeshsolanki