Does anyone know why I'm still receiving a deprecation warning even though I've already specified useUnifiedTopoology: true
in my MongoClient
constructor?
Thank you in advance!
const mongodb = require('mongodb')
const MongoClient = mongodb.MongoClient
const connectionURL = 'connectionurl'
const databaseName = 'db'
const client = new MongoClient(connectionURL, { useNewUrlParser: true, useUnifiedTopology: true});
const insertHandler = async(data, collectionName) => {
await client.connect().then(async() => {
const collection = client.db(databaseName).collection(collectionName)
await collection.insertOne(data)
}).catch(error => {
console.log("Failed to insert:", error)
})
}
module.exports = {
insertHandler: insertHandler
}
And I'm getting the following error:
DeprecationWarning: current Server Discovery and Monitoring engine
is deprecated, and will be removed in a future version. To use the
new Server Discover and Monitoring engine, pass option { useUnifiedTopology:
true } to the MongoClient constructor.
useUnifiedTopology: False by default. Set to true to opt in to using the MongoDB driver's new connection management engine. You should set this option to true , except for the unlikely case that it prevents you from maintaining a stable connection.
Description. MongoClient() Initializes a new instance of the MongoClient class. MongoClient(String) Initializes a new instance of the MongoClient class.
MongoClient is the name of a class that you imported from the mongodb package. MongoClient. connect() is a static method of that class. It creates an actual instance of MongoClient (your client object) and passes it to your callback.
the useCreateIndex option ensures that you are using the new function calls. Reference: https://mongoosejs.com/docs/connections.html#options https://mongoosejs.com/docs/deprecations.html.
I'm using Mongoose for MongoDB, the same code with no error. mongoose.connect("mongodb://localhost:27017/YOURDB", { useNewUrlParser: true, useUnifiedTopology: true });
You can do it this way
var mongoDb = require('mongodb');
var mongoClient = mongoDb.MongoClient;
var serverUrl = "mongodb://127.0.0.1:27017/";
var dbName = "sample_db";
mongoClient.connect(serverUrl, { useNewUrlParser: true, useUnifiedTopology: true }, function (err, db) {
// Code goes here...
});
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