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?
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').
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.
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.
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.
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.
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.
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.
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!
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With