Mongoose 如何连接多个数据库

问题描述 投票:0回答:4

在一个项目中,我需要在一些MongoDB数据库之间切换来进行一些查询。

我发现了一些类似的例子:Mongoose 和单个 Node.js 项目中的多个数据库

他的答案完全有效,但现在我试图循环执行它,不幸的是它不起作用,我收到此错误:

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: failed to connect to [localhost:27017]
    at null.<anonymous> (/home/user/test_many_db_mongodb/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:549:74)
    at EventEmitter.emit (events.js:106:17)
    at null.<anonymous> (/home/user/test_many_db_mongodb/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:150:15)
    at EventEmitter.emit (events.js:98:17)
    at Socket.<anonymous> (/home/user/test_many_db_mongodb/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:533:10)
    at Socket.EventEmitter.emit (events.js:95:17)
    at net.js:440:14
    at process._tickCallback (node.js:419:13)

我不明白为什么会出现此错误,如果上面链接中的示例有效,我的代码也应该:

var mongoose = require('mongoose');

for (var i = 0; i != 1000; i++) {
    var conn = mongoose.createConnection('mongodb://localhost/test' + i);

    conn.on('connected', function() {
        console.log('Mongoose connected to database');

        var Model    = conn.model('Model', new mongoose.Schema({
            title : { type : String, default : 'model in test database' }
        }));

        var newModelA = new Model();
        newModelA.save(function(err) {
            if (err)
                console.log(err);
            console.log('save A');
        });
    });
}

感谢您的帮助。

node.js mongodb mongoose multiple-databases
4个回答
1
投票

我认为for循环有问题。因为循环内的代码本质上是异步的。所以我用了 forEach 而不是 for。请看下面的代码。对我来说效果很好。

var mongoose = require('mongoose'),
    arr = [0, 1, 2];

arr.forEach(function(i) {
    var conn = mongoose.createConnection('mongodb://localhost/test' + i);

    conn.on('connected', function() {
        console.log('Mongoose connected to database', i);

        var Model = conn.model('Model', new mongoose.Schema({
            title: {
                type: String,
                default: 'model in test database'
            }
        }));

        var newModelA = new Model();
        newModelA.save({
            name: 'a'
        }, function(err) {
            if (err)
                console.log(err);
            console.log('save A');
        });
    });
});

我已经这样做了三遍了。你也可以做1000次。


1
投票

您收到

failed to connect
错误,因为您正在同步循环中创建所有 1000 个数据库连接池,这耗尽了可用 MongoDB 连接的供应。

因此,您需要使用类似

async
库的
eachLimit
方法的方法来引入一些异步流控制:

var mongoose = require('mongoose');
var async = require('async');

var iterations = [];
for (var i = 0; i != 1000; i++) {
    iterations.push(i);
}

// Iterate over iterations, allowing no more than 5 async iterations to be
// outstanding at any one time.
async.eachLimit(iterations, 5, function(i, callback) {
    var conn = mongoose.createConnection('mongodb://localhost/test' + i);

    conn.on('connected', function(err) {
        console.log('Mongoose connected to database');

        var Model = conn.model('Model', new mongoose.Schema({
            title : { type : String, default : 'model in test database' }
        }));

        var newModelA = new Model();
        newModelA.save(function(err) {
            if (err)
                console.log(err);
            console.log('save A');

            // Close the connection and tell eachLimit this iteration is
            // complete by having the close method call the eachLimit callback
            // when the close completes.
            conn.close(callback);
        });    
    });
}, function() {
    console.log('All done!');
});

0
投票

感谢您的帮助。

我尝试了两种方法,但它在我的项目中不起作用,可能是因为我没有向您展示所有代码。我找到了一种使用 useDb():

的方法
var amqp = require('amqp');

var MailParser = require('mailparser').MailParser;
var mailparser = new MailParser();

var mongoose = require('mongoose');
var conn = mongoose.createConnection('mongodb://localhost');

var count = 1;

var connection = amqp.createConnection({host: 'localhost'});

    connection.on('ready', function() {
        connection.queue('task_queue', {autoDelete: false,
                                        durable: true}, function(queue) {

            console.log('Waiting for emails. To exit press CTRL+C');

            queue.subscribe({ack: true, prefetchCount: 1}, function(message, headers, deliveryInfo, ack) {
                mailparser.write(message.data.toString('utf-8'));
                mailparser.end();

                ack.acknowledge();
            });
        });
    });

mailparser.on("end", function(email_object){
    var d = new Date(); // just for be sure the db name is unique for the test
    var db = conn.useDb('test_'+d.getDate()+'-'+d.getMonth()+'-'+d.getYear()+'_'+d.getHours()+'-'+d.getMinutes()+'-'+d.getSeconds()+'-'+d.getMilliseconds());

    var Model    = conn.model('Model', new mongoose.Schema({
            subject : { type : String },
            body : { type : String }
        }));
    var newEmail = new Model();
    newEmail.subject = email_object.subject;
    newEmail.body = email_object.body;
    newEmail.save(function(err) {
        if (err) console.error(err);
        console.log(count++);
    });
});

0
投票

我为此创建了一个简单的包。 mongoplusplus

导入

const mongoplusplus = require('mongoplusplus');

初始化中

const dbname = 'testforUP';

const mongoURI1 = `mongodb+srv://xxxxx:[email protected]/${dbname}?retryWrites=true&w=majority`;
const mongoURI2 = `readonly:mongodb+srv://xxxxxxx:[email protected]/${dbname}?retryWrites=true&w=majority`;
const mongodb = new mongoplusplus([mongoURI1, mongoURI2]);

readonly:
用于将数据库标记为只读。 ([mongoURI1, mongoURI2]);

连接数据库

这是主页中的顶层内容(在此测试代码中,在

mongodb
变量声明下)

const mongodb = new mongoplusplus([mongoURI1, mongoURI2]);

(async () => {
  await mongodb.connectToAll();
})();

大部分内容都基于 mongoose 包,因此如果您遵循文档,您就会理解一切。

文档

© www.soinside.com 2019 - 2024. All rights reserved.