在mongo / mongoose中一次连接到许多数据库

时间:2016-08-08 15:08:06

标签: node.js mongodb mongoose

我们正在尝试使用mongoose连接到1500个数据库,但使用此命令创建1500个连接1500个数据库的速度太慢

mongoose.createConnection(url);

1500 DB位于同一数据库服务器上。 建立这些联系花了50多分钟。

有没有办法减少时间,或者有没有办法连接到1500 DB,因为它们在同一台服务器上?

1 个答案:

答案 0 :(得分:1)

您可以尝试async

'use strict';
const mongoose = require('mongoose'),
    async = require('async'),
    dbsUrl = [
    'mongodb://url1',
    //...
    'mongodb://url15000',
];

async.map(dbsUrl, (url, callback) => {
    let conn = mongoose.createConnection();
    conn.once('error', (err) => {
        callback(err);
    });
    conn.once('connected', () => {
        callback(null, conn);
    });
}, (err, dbs) => {
    //If a error happenned, it will callback immediately
    //Else, dbs will now be a array of connections
});

我不知道有多少连接的性能。

相关问题