- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我尝试打包数据库连接以提高可重用性。我想实现这样的目标:
const mongoPromise =MongoClient.connect(url,{ useNewUrlParser: true })
.then((client)=>{
const db = client.db(dbName);
// do something...
client.close();
})
.catch(err=>console.log(err));
因此,我可以将它用在其他地方:
//For example
//query
mongoPromise.then((db)=>db.collection('user').find().toArray())
//insert
mongoPromise.then((db)=>db.collection('test').insert({...}))
当查询或插入完成后,MongoClient将关闭
第一种方法,我可以通过混合回调和 promise 来找出解决方案。
将回调和 Promise 混合在一起不是很好吗?
// First method
const mongoPromiseCallback =(callback)=>MongoClient.connect(url,{ useNewUrlParser: true })
.then(async(client)=>{
const db = client.db(dbName);
await callback(db);
console.log("close the client");
client.close();
})
.catch(err=>console.log(err))
mongoPromiseCallback(db=>db.collection('user').find().toArray())
.then(res=>console.log(res)));
在另一种方法中,我尝试仅使用 promise ,但我不知道我可以在哪里关闭客户端。
// the other method
const mongoPromise =MongoClient.connect(url,{ useNewUrlParser: true })
.then((client)=>{
const db = client.db(dbName);
return new Promise(function(resolve, reject) {
resolve(db);
});
})
.catch(err=>console.log(err));
mongoPromise.then(db=>db.collection('user').find().toArray())
.then(res=>console.log("res"));
最佳答案
您始终可以重用在 mongo 中创建的数据库对象。阅读本文,它将回答问题 How do I manage MongoDB connections in a Node.js web application?
关于javascript - 如何使用MongoClient而不需要每次都编码 "client.close()"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56548104/
const { MongoClient, ObjectID } = require('mongodb'); const debug = require('debug')('mongodb-connec
当我启动我的一些服务时,它会报告此类警告并且服务会停止: /usr/lib64/python2.6/site-packages/pymongo/topology.py:75: UserWarning:
我不喜欢新的 mongo,MongoDB 在 PHP7 中需要几个库。 PHP 5 中的 MongoClient(已弃用)更加舒适和轻便! 我决定启动一个脚本并比较两个版本,结果非常令人惊讶: Mon
我正在尝试实现 MongoDB 连接处理程序,但遇到了一个问题,我的 Database即使我的 MongoClient() 调用成功,调用也会返回 None。 #mongoconn.py from p
我对 MongoDB 有点陌生,我对 MongoClient 类感到困惑,因为在不同的包中有两个( com.mongodb.client.MongoClient 和 com.mongodb.Mongo
我正在尝试熟悉从 C# 程序写入 MongoDB。我已经按照 http://mongodb.github.io/mongo-csharp-driver/1.11/getting_started/ 的建
我正在使用 Celery 和 MongoEngine 作为我的 Django 应用程序的一部分。 当 celery @shared_task 通过 mongoengine 模型类访问 mongodb
我有一个非常简单的 PyMongo 配置,连接到两个主机: from pymongo import MongoClient host = os.getenv('MONGODB_HOST', '127.
这个问题已经有答案了: MongoDB - Java | How to manage the connection (1 个回答) 已关闭 5 年前。 我正在用 Java 开发一个应用程序,该应用程序
我有一个副本集设置,其中包含 1 个主节点 (mongo1.test.com)、1 个辅助节点 (mongo2.test.com) 和 1 个仲裁节点 (mongo3.test.com)。当我使用 M
我是 NodeJS 新手,也是 JS can-kicker 第一次尝试 DI。以下是我在决定问我之前看过的问题,因为它们显示相同的错误:[1] [2] 运行我的入口点产生: this.client =
使用 java MongoClient 库,如何在集合中查找文档并仅返回特定的对象?我知道这对于 1 个对象 是可能的,但不确定多个。 对于 1 个对象: DBCursor cursor = db.g
这是我的服务器告诉我的内容: MongoClient.connect('mongodb://:@ds235778.mlab.com:35778/satr-wars-quotes', (err, cli
我有一个使用 MongoDB 作为数据库的 ASP.Net MVC 应用程序。网站和数据库位于不同的服务器上。 目前,我有一个看起来像这样的类: public class Mongo { pr
我无法做到这一点: from pymongo import MongoClient 我明白了: >>> import pymongo >>> from pymongo import MongoClie
我正在尝试让这段代码运行: tilbud; ?> 每次我遇到同样的错误: Fatal error: Class 'MongoClient' not found in C:\xampp\htdocs\c
我正在努力更好地理解 this documentation关于如何重用 MongoClient 的实例。 我们通过在构造函数中传递连接字符串来创建 MongoClient 实例,this is the
我正在使用 Mongo DB java 驱动程序连接到 mongo 实例。下面是我用来创建 MongoClient 实例的代码。 try { new MongoClient("
什么是 Mongoclient?它在下面的语句中做了什么? var MongoClient = require('mongodb').MongoClient; 最佳答案 您示例中的 MongoClie
假设我有一个副本集,其中包含一个主 P 和三个辅助 S1、S2 和 S3 。假设应用程序使用此 constructor 创建一个 Mongo 客户端。 。 种子列表为{P、S1、S2}。请注意,该列表
我是一名优秀的程序员,十分优秀!