javascript - 通过选项 { useUnifiedTopology : true } to the MongoClient constructor

标签 javascript reactjs database mongodb mongoose

有谁知道为什么即使我已经指定了 useUnifiedTopoology: true,我仍然会收到弃用警告在我的MongoClient构造函数?
先感谢您!

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
}
我收到以下错误:
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.
enter image description here
enter image description here

最佳答案

你可以这样做

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...
});

关于javascript - 通过选项 { useUnifiedTopology : true } to the MongoClient constructor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61266601/

相关文章:

javascript - 使用正则表达式或其他方式替换字符串中的数字

javascript - 根据目标距离计算 iOS 上的到达时间

python - 导入 "rest_framework"无法解析。但是我已经安装了djangorestframework,不知道哪里出了问题

database - 如何在非常大的图中添加边(关系)Neo4j

javascript - 如何访问特定的 Web 元素 Selenium Python

javascript - 如何在页面中间设置成功提醒?

javascript - 通过使用 react-select 映射对象数组来生成选项

javascript - 从 React fetch 调用组件方法

database - Laravel 4 多对多与 Eloquent 和复选框

android - 如何使用 SQL 数据库在 Android 中保存从 Arduino 板接收到的数据?