84

在许多使用 MongoDB 的介绍性示例中,您会看到如下代码:

var MongoClient = require('mongodb').MongoClient;
MongoClient.connect("mongodb://localhost:port/adatabase", function(err, db)
{
    /* Some operation... CRUD, etc. */
    db.close();
});

如果 MongoDB 与任何其他数据库系统一样,open并且close操作通常在时间上很昂贵。

所以,我的问题是:是否可以简单地做MongoClient.connect("...一次,将返回的db值分配给某个模块全局,让模块中的各种功能做各种与数据库相关的工作(将文档插入集合,更新文档等)。 ) 当它们被应用程序的其他部分调用时(从而重用该db值),然后,当应用程序完成时,才执行close.

换句话说,open并且close只做一次——不是每次你都需要去做一些与数据库相关的操作。并且您继续重新使用db在 initial 期间返回的该对象open\connect,只是在最后使用close, 当您实际完成所有与数据库相关的工作时处理它。

显然,由于所有 I/O 都是异步的,因此close在发出close. 看起来这应该没问题,但我想仔细检查以防万一我错过了一些东西,因为我是 MongoDB 的新手。谢谢!

4

3 回答 3

67

是的,这是很好的典型行为。启动您的应用程序,连接到数据库,长时间对数据库执行操作,如果连接意外终止,则可能重新连接,然后永远不要关闭连接(只需依赖进程终止时发生的自动关闭) .

于 2013-09-06T06:21:58.960 回答
14

MongoDB版本^3.1.8

将连接初始化为承诺:

const MongoClient = require('mongodb').MongoClient
const uri = 'mongodb://...'
const client = new MongoClient(uri)
const connection = client.connect() // initialized connection

然后在您希望对数据库执行操作时调用连接:

    // if I want to insert into the database...
    const connect = connection
    connect.then(() => {
        const doc = { id: 3 }
        const db = client.db('database_name')
        const coll = db.collection('collection_name')
        coll.insertOne(doc, (err, result) => {
            if(err) throw err
        })
    })
于 2018-10-13T08:00:39.197 回答
5

当前接受的答案是正确的,因为您可以保持相同的数据库连接打开以执行操作,但是,它缺少有关在关闭时如何重试连接的详细信息。以下是自动重新连接的两种方法。它在 TypeScript 中,但如果需要,它可以很容易地转换成普通的 Node.js。

方法一:MongoClient 选项

允许 MongoDB 重新连接的最简单方法是在将 areconnectTries传递optionsMongoClient. 任何时候 CRUD 操作超时,它都会使用传入的参数MongoClient来决定如何重试(重新连接)。将选项设置为Number.MAX_VALUE实质上使其永远重试,直到它能够完成操作。如果您想查看将重试哪些错误,可以查看驱动程序源代码。

class MongoDB {
    private db: Db;

    constructor() {
        this.connectToMongoDB();
    }

    async connectToMongoDB() {
        const options: MongoClientOptions = {
            reconnectInterval: 1000,
            reconnectTries: Number.MAX_VALUE
        };

        try {
            const client = new MongoClient('uri-goes-here', options);
            await client.connect();
            this.db = client.db('dbname');
        } catch (err) {
            console.error(err, 'MongoDB connection failed.');
        }
    }

    async insert(doc: any) {
        if (this.db) {
            try {
                await this.db.collection('collection').insertOne(doc);
            } catch (err) {
                console.error(err, 'Something went wrong.');
            }
        }
    }
}

方法二:Try-catch Retry

如果您想在尝试重新连接时获得更精细的支持,可以使用带有 while 循环的 try-catch。例如,您可能希望在必须重新连接时记录错误,或者您希望根据错误类型执行不同的操作。这也将允许您根据更多条件重试,而不仅仅是驱动程序随附的标准条件。该insert方法可以更改为以下内容:

async insert(doc: any) {
    if (this.db) {
        let isInserted = false;

        while (isInserted === false) {
            try {
                await this.db.collection('collection').insertOne(doc);
                isInserted = true;
            } catch (err) {
                // Add custom error handling if desired
                console.error(err, 'Attempting to retry insert.');

                try {
                    await this.connectToMongoDB();
                } catch {
                    // Do something if this fails as well
                }
            }
        }
    }
}
于 2020-03-12T21:19:25.327 回答