我正在尝试编写一个使用Nodejs上Mongo的本地客户端的小类,但是当我尝试关闭连接,然后再次打开(使用相同的客户端对象)时,操作失败,并指出连接已关闭(但我刚刚打开)。
那么会发生什么呢?
const { MongoClient } = require('mongodb');
let client = new MongoClient(urlMongoDbInstance)
await client.connect();
console.log(await client.db.(dbName).collection().findOne({}));
// [CONSOLE]
// prints object
client.close();关闭连接后,我尝试打开它。
await client.connect();
/* [CONSOLE]
the options [servers] is not supported
the options [caseTranslate] is not supported
the options [dbName] is not supported
the options [mode] is not supported
the options [tags] is not supported
the options [hedge] is not supported
the options [preference] is not supported
the options [isValid] is not supported
the options [slaveOk] is not supported
the options [equals] is not supported
the options [toJSON] is not supported
*/然后尝试使用数据库连接
console.log(await client.db.(dbName).collection().findOne({}));
// [CONSOLE]
// MongoError: Topology is closed, please connect我知道(或认为我知道)或发现了什么
我知道这是一个很好的做法,在应用程序运行时保持连接打开,但这与此无关。
我已经尝试创建一个新的变量来连接每次并工作(但这不是这里的想法)。
我在Mongo上找到了一个关于它的条目(似乎是同样的问题) https://jira.mongodb.org/browse/NODE-2544
有问题吗?
有没有人在这个问题上找到了解决办法,也许我没有像必须使用的那样使用驱动程序?
发布于 2020-10-05 09:16:48
看起来这是节点驱动程序中的一个已知问题。
您应该能够通过从头开始重新创建客户端对象来解决这个问题(而不是尝试重新连接断开连接的现有客户端)。
发布于 2020-10-05 09:30:49
也许这不是一个问题(或部分问题),我到达了reference接口并查看了db对象的可用选项
db(dbName, options)
创建一个共享当前套接字连接的新Db实例。请注意,新的数据库实例以父子关系与原始实例相关,以便在子数据库实例上正确发出事件。子数据库实例被缓存,因此执行两次db('db1')将返回相同的实例。可以使用选项noListener和returnNonCachedInstance控制这些行为。
则可能是缓存实例解析关闭的连接,因此使用db作为
await client.db(dbName, {returnNonCachedInstance : true});像预期的那样工作,,它给我消息,但似乎不会干扰。
the options [servers] is not supported
the options [caseTranslate] is not supported
the options [dbName] is not supported
the options [mode] is not supported
the options [tags] is not supported
the options [hedge] is not supported
the options [preference] is not supported
the options [isValid] is not supported
the options [slaveOk] is not supported
the options [equals] is not supported
the options [toJSON] is not supported谢谢!
https://stackoverflow.com/questions/64201287
复制相似问题