【问题标题】:Mongoose, "the options [port] is not supported"Mongoose,“不支持选项 [端口]”
【发布时间】:2019-09-07 03:53:41
【问题描述】:

任何人都知道为什么我在使用 mongoose 时总是在控制台日志中看到这个

the options [port] is not supported

快速上下文:

  • 在本地运行mongod
  • 在本地运行 node/express 应用程序
  • “猫鼬”:“^5.6.11”
  • mongodb v4.0.10
  • 下面的代码 sn-p:

const mongoProfile = require('../config/mongo-profile.json');
let mongooseConnPromise = mongoose.connect(mongoProfile.url, mongoProfile.connectionOptions);
let dbConn = mongoose.connection;
dbConn.on('error', console.error.bind(console, 'connection error:'));

dbConn.on('open', () => {
    RpDataModel.find({$or: queryData},
    (err, docs) => {
      dbConn.close();
      res.append('ETag', `My PerfTest, ${new Date(Date.now())}`);
      res.json(docs);
  });

});

"('../config/mongo-profile.json')" ???? conn str 和下面的选项

{
    "url": "mongodb://localhost",
    "connectionOptions": {
      "useNewUrlParser": true,
      "port": 27017,
      "user": "rpTest",
      "dbName":"rp-db-perf-test",
      "pass":"...",
      "keepAlive": true,
      "keepAliveInitialDelay": 0
    }
}

【问题讨论】:

    标签: node.js mongodb express mongoose


    【解决方案1】:

    我不是 Mongoose 专家,但根据文档,port 不是有效选项:

    Mongoose 使用 Node.js MongoDB 驱动程序。这是available connection options的列表。

    根据Mongoose docs,正确的连接方式是使用MongoDB Standard Connection String Format,即:

    mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[database][?options]]
    

    您可以在 Mongoose 的 Connections 文档中查看一些示例。

    据我了解,您可以将mongo-profile.json 更改为:

    {
        "url": "mongodb://localhost",
        "port": 27017,
        "user": "rpTest",
        "dbName":"rp-db-perf-test",
        "pass":"...",
        "connectionOptions": {
          "useNewUrlParser": true,
          "keepAlive": true,
          "keepAliveInitialDelay": 0
        }
    }
    

    你的连接代码:

    const mongoProfile = require('../config/mongo-profile.json');
    var url = mongoProfile.url;
    var port = mongoProfile.port;
    var dbName = mongoProfile.dbName;
    var user = mongoProfile.user;
    var pass = mongoProfile.pass;
    
    let mongooseConnPromise = mongoose.connect(
        'mongodb://' + user + ':' + pass + '@' + url + ':' + port + '/' + dbName, 
        mongoProfile.connectionOptions
    );
    let dbConn = mongoose.connection;
    dbConn.on('error', console.error.bind(console, 'connection error:'));
    

    【讨论】:

      【解决方案2】:

      根据文档,即使您使用的是最旧版本的 mongoose (3.8),也不能指定这样的端口。根据文档的唯一方法是将它传递到连接字符串中,如下所示:

      mongoose.connect('mongodb://host:port');
      

      但是由于您使用的是带有默认端口的 localhost,因此您甚至不需要在此处指定端口:

      mongoose.connect('mongodb://host');
      

      因此,只需从配置文件中删除参数即可。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-06
        • 2011-12-22
        • 1970-01-01
        • 1970-01-01
        • 2018-09-01
        • 2021-10-25
        • 2018-06-10
        • 1970-01-01
        相关资源
        最近更新 更多