0

我正在尝试使用 dart 包 mongo_dart 写入 Atlas mongoDB 云服务。

这是如何连接到:

  static const String uri =
      "mongodb+srv://User:Password@Project.e0nro.mongodb.net/DbName?retryWrites=true&w=majority&tls=true";

  Future<Db> connectToDatabase() async {
    var db = await Db.create(uri);
    await db.open(secure: true);
    print('Connected to database');
    return db;
  }

这就是如何更新我的数据:

await coll.update(
            {
              "symbol": symbol,
            },
            {
              r"$addToSet": {"pens": pen.toJson()}
            },
          );

我很快就遇到了这个错误:

Unhandled exception:
MongoDB ConnectionException: connection closed: The socket connection has been reset by peer.
Possible causes:
- Trying to connect to an ssl/tls encrypted database without specifiyng
  either the query parm tls=true or the secure=true parameter in db.open()
- Others

有时这个:

Unhandled exception:
MongoDB ConnectionException: connection closed: SocketException: Read failed (OS Error: Connection reset by peer, errno = 104), address = ????.e0nro.mongodb.net, port = 52508

在 Atlas 面板中,我添加了 IP 地址以全局访问数据库

0.0.0.0/0  (includes your current IP address)   access  Active

我该如何解决这些错误?

4

0 回答 0