You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mongoose从v5.10.0迁移至v6.7.2时遇Invalid URL错误求助

Mongoose v6.7.2 连接MongoDB时出现URL无效错误

我正在将Mongoose从v5.10.0版本迁移至v6.7.2版本,参考了Mongoose官方v6迁移指南以及MongoDB Node.js驱动4.0版本变更说明。我的MongoDB集群是3节点副本集,使用的连接配置如下:

let options = {
        serverSelectionTimeoutMS: 5000,
        retryWrites : true,
        appName: 'testAppName',
        replicaSet: 'test-replicaset',
        maxIdleTimeMS: 150000, 
        wtimeoutMS: 0, 
        w: "majority", 
        autoIndex: false,
        maxPoolSize: 10,
        keepAlive: true, 
        connectTimeoutMS: 60000
    };

通过以下代码建立连接:

mongoose.connect(mongo_server_uri + '/' + database, options);

其中mongo_server_uri为mongodb+srv://username:password@development.xyz.mongodb.net,database值为'abc',最终拼接后的连接语句为:

mongoose.connect('mongodb+srv://username:password@development.xyz.mongodb.net/abc',options)

当前环境信息:

  • Mongoose: 6.7.2
  • MongoDB Server: 4.4.17
  • Node.js: 16.16.0
  • macOS: Monterey v12.5.1

运行时抛出如下错误:

TypeError: Invalid URL: mongodb+srv://username:password@development.xyz.mongodb.net/abc
      at new URLImpl (/root_path/node_modules/whatwg-url/lib/URL-impl.js:21:13)
      at Object.exports.setup (/root_path/node_modules/whatwg-url/lib/URL.js:54:12)
      at new URL (/root_path/node_modules/whatwg-url/lib/URL.js:115:22)
      at new URLWithoutHost (/root_path/node_modules/mongodb-connection-string-url/lib/index.js:74:1)
      at new ConnectionString (/root_path/node_modules/mongodb-connection-string-url/lib/index.js:126:13)
      at parseOptions (/root_path/node_modules/mongodb/lib/connection_string.js:200:17)
      at new MongoClient (/root_path/node_modules/mongodb/lib/mongo_client.js:45:63)
      at /root_path/node_modules/mongoose/lib/connection.js:801:16
      at Promise._execute (/root_path/node_modules/bluebird/js/release/debuggability.js:384:9)
      at Promise._resolveFromExecutor (/root_path/node_modules/bluebird/js/release/promise.js:518:18)
      at new Promise (/root_path/node_modules/bluebird/js/release/promise.js:103:10)
      at NativeConnection.Connection.openUri (/root_path/node_modules/mongoose/lib/connection.js:798:19)
      at Mongoose.createConnection (/root_path/node_modules/mongoose/lib/index.js:359:10)
      at module.exports (/root_path/index.js:125:27)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
      at async /root_path/bin/something.js:14:15,
  _rejectionHandler0: undefined,
  _promise0: undefined,
  _receiver0: undefined
} reason: TypeError: Invalid URL: mongodb+srv://username:password@development.xyz.mongodb.net/abc

恳请各位提供解决思路,谢谢!

内容的提问来源于stack exchange,提问作者Pawan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 02:35:24