【问题标题】:Loopback - Unable to connect to Mongo DB Atlas using mongodb+srv Protocol from loopback环回 - 无法使用来自环回的 mongodb+srv 协议连接到 Mongo DB Atlas
【发布时间】:2019-12-27 17:53:47
【问题描述】:

无法使用 mongodb+srv 协议连接到 Mongo DB Atlas。 在尝试查询一些数据时- MongoError:无法在 atlas 中对 admin 进行原始查询

我在 datasource.json 中尝试过的连接属性

mongoDb: {
connector: 'mongodb',
name: 'mongoDb',
url:"mongodb+srv://#username:#password@cluster0-kvw6a.mongodb.net/test?retryWrites=true&w=majority"
}

在尝试查询某些数据时 - 收到此错误

Unhandled error for request GET /api/Customers/getProfile?access_token=cMGG6C8fB8el0vbchpAzeOoTpH0N7Kf9va4b5k9Z6GrNAwgh4AtqQKlRYOyiWtsB: MongoError: cannot do
raw queries on admin in atlas
at Function.MongoError.create (C:\Users\User\Documents\GitHub\maharah\customer_server\node_modules\mongodb-core\lib\error.js:31:11)
at queryCallback (C:\Users\User\Documents\GitHub\maharah\customer_server\node_modules\mongodb-core\lib\cursor.js:212:36)
at C:\Users\User\Documents\GitHub\maharah\customer_server\node_modules\mongodb-core\lib\connection\pool.js:469:18
at process._tickCallback (internal/process/next_tick.js:61:11)

我在 datasource.json 中尝试过的连接属性

mongoDb: {
connector: 'mongodb',
name: 'mongoDb',
url:"mongodb+srv://#username:#password@cluster0-kvw6a.mongodb.net/test?
retryWrites=true&w=majority"
}

【问题讨论】:

  • 请花一些时间来正确格式化您的代码

标签: node.js mongodb loopbackjs mongodb-atlas


【解决方案1】:

我们在今年 4 月改进了对mongodb+srv 协议的支持,请参阅PR #497。您需要使用loopback-connector-mongodb 版本4.2.0 或更高版本。

引用README.md#using-the-mongodbsrv-protocol:

MongoDB 支持一种称为mongodb+srv 的协议,用于连接到副本集,而无需提供副本集中每个服务器的主机名。 要将mongodb+srv 用作协议,请将datasource.json 中的protocol 连接属性设置为mongodb+srv。例如:

"mydb": {
  "host": "myserver",
  "database": "test",
  "protocol": "mongodb+srv",
  "connector": "mongodb"
}

注意:使用mongodb+srv协议时未指定端口,如果给出将被忽略。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-27
    • 2018-06-13
    • 1970-01-01
    • 2019-04-04
    • 2020-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多