首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >第一次关闭后无法重用node-mongodb-native客户端

第一次关闭后无法重用node-mongodb-native客户端
EN

Stack Overflow用户
提问于 2020-10-05 08:57:28
回答 2查看 139关注 0票数 1

我正在尝试编写一个使用Nodejs上Mongo的本地客户端的小类,但是当我尝试关闭连接,然后再次打开(使用相同的客户端对象)时,操作失败,并指出连接已关闭(但我刚刚打开)。

那么会发生什么呢?

代码语言:javascript
复制
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();

关闭连接后,我尝试打开它。

代码语言:javascript
复制
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
*/

然后尝试使用数据库连接

代码语言:javascript
复制
console.log(await client.db.(dbName).collection().findOne({}));
// [CONSOLE]
// MongoError: Topology is closed, please connect

我知道(或认为我知道)或发现了什么

我知道这是一个很好的做法,在应用程序运行时保持连接打开,但这与此无关。

我已经尝试创建一个新的变量来连接每次并工作(但这不是这里的想法)。

我在Mongo上找到了一个关于它的条目(似乎是同样的问题) https://jira.mongodb.org/browse/NODE-2544

有问题吗?

有没有人在这个问题上找到了解决办法,也许我没有像必须使用的那样使用驱动程序?

EN

回答 2

Stack Overflow用户

发布于 2020-10-05 09:16:48

看起来这是节点驱动程序中的一个已知问题。

您应该能够通过从头开始重新创建客户端对象来解决这个问题(而不是尝试重新连接断开连接的现有客户端)。

票数 1
EN

Stack Overflow用户

发布于 2020-10-05 09:30:49

也许这不是一个问题(或部分问题),我到达了reference接口并查看了db对象的可用选项

db(dbName, options)

创建一个共享当前套接字连接的新Db实例。请注意,新的数据库实例以父子关系与原始实例相关,以便在子数据库实例上正确发出事件。子数据库实例被缓存,因此执行两次db('db1')将返回相同的实例。可以使用选项noListenerreturnNonCachedInstance控制这些行为。

则可能是缓存实例解析关闭的连接,因此使用db作为

代码语言:javascript
复制
await client.db(dbName, {returnNonCachedInstance : true});

像预期的那样工作,,它给我消息,但似乎不会干扰。

代码语言:javascript
复制
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

谢谢!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64201287

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档