简体   繁体   中英

Connecting to many databases at once in mongo/mongoose

We are trying to connect to 1500 databases using mongoose but it's too slow to create 1500 connection with 1500 DB using this command

mongoose.createConnection(url);

the 1500 DB are on the same database server. it took more than 50 minutes to establish these connections.

Is there any way to decrease the amount of time or is there a way to connect to the 1500 DB at once as they are on the same server?

You could try 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
});

I do not know about the performance for such number of connection though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM