第一次关闭后无法重用node-mongodb-native客户端 那怎么了?我知道或认为我知道或发现的东西问题?

问题描述

我正在尝试编写一个使用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

问题?

有没有人找到解决方法,也许我没有像必须使用的那样使用驱动程序?

解决方法

这似乎是节点驱动程序中的一个已知问题。

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

,

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

db(dbName,options)

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

然后它可能是解析关闭连接的缓存实例,因此使用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

谢谢!

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...