使用 Node.js 20.3.1 连接 MongoDB Atlas 时收到 DEP0170

Tai*_*nia 5 mongoose mongodb node.js mongodb-atlas

最近我将 Node.js 更新到版本 20.3.1 并尝试连接我的 MongoDB Atlas。当我的Node.js版本为19.X时,Atlas连接正常。但是,终端在 Node.js 20.3.1 中显示弃用警告:

[DEP0170] DeprecationWarning: The URL mongodb://<USERNAME>:<PASSWORD>@ac-4hlp44x-shard-00-01.nqjamzn.mongodb.net:27017,ac-4hlp44x-shard-00-02.nqjamzn.mongodb.net:27017,ac-4hlp44x-shard-00-00.nqjamzn.mongodb.net:27017/<DATABASE>?authSource=admin&replicaSet=atlas-g8a2vr-shard-0&retryWrites=true&w=majority&ssl=true is invalid. Future versions of Node.js will throw an error.
Run Code Online (Sandbox Code Playgroud)

连接 Altas 的原始 url 是:

MONGODB_URI=mongodb+srv://<USERNAME>:<PASSWORD>@<USERNAME>.nqjamzn.mongodb.net/<DATABASE>?retryWrites=true&w=majority
Run Code Online (Sandbox Code Playgroud)

数据库仍处于连接状态。

我尝试搜索相关文章,但没有任何有关此弃用的信息。

我该如何修复它?谢谢!

小智 4

我看到了同样的事情,并查看了节点折旧文档(https://nodejs.org/api/deprecations.html#DEP0170),这意味着它不喜欢非数字端口。因此,我认为由于 Mongo Atlas 连接字符串往往包含大量“:”字符(例如mongodb+srv://<USERNAME>:<PASSWORD>),它认为这些应该是端口号。

尚不确定解决方案,但希望这会有所帮助。