mongo.isConnected 节点驱动程序 v4+ 的替代方案

发布于 2025-01-16 16:34:37 字数 383 浏览 1 评论 0原文

原生 Node JS MongoDB 驱动程序中已弃用的 MongoClient.isConnected 是否有任何替代方案?

在某处读到可以通过以下方式处理检查回调,可以在 .connect 方法中作为参数传递 (请参阅文档。) 但是,我没能成功做到这一点。

Is there any alternative for the deprecated MongoClient.isConnected in the native Node JS MongoDB driver?

I read somewhere that one can handle the check via callbacks, which one can pass as parameters in the .connect Method (See the docs.)
However, I wasn't able to successfully do that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心奴独伤 2025-01-23 16:34:37

您可以使用 mongoose.connect() 方法连接到 MongoDB。

mongoose.connect('mongodb://localhost:27017/myapp'); 

使用 npm 从命令行安装 Mongoose:

$ npm install mongoose --save

请参阅我的 MongoDB + Node 示例:

https://github.com/juhisri211/node-mongo-app/blob/master/index.js

有关 Mongoose 的更多详细信息,请参考官方文档:

https://mongoosejs.com/docs/index.html

You can connect to MongoDB with the mongoose.connect() method.

mongoose.connect('mongodb://localhost:27017/myapp'); 

Install Mongoose from the command line using npm:

$ npm install mongoose --save

Refer to my MongoDB + Node example:

https://github.com/juhisri211/node-mongo-app/blob/master/index.js

For more details about Mongoose, please refer to official documentation:

https://mongoosejs.com/docs/index.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文