Connect to many databases at once in mongo / mongoose

We are trying to connect to 1500 databases using mongoose, but too slow to create 1500 connections with 1500 DB using this command

mongoose.createConnection(url);

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

Is there a way to reduce the amount of time, or is there a way to connect to 1500 DB right away, since they are on the same server?

+4
source share
1 answer

You can 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 don't know about performance for so many connections, though.

+1
source

Source: https://habr.com/ru/post/1650567/


All Articles