MongoDB / NodeJS:每个variables的最大连接数/数据库创build

我需要你的帮助,使用mongodb允许的最大连接数。 我得到这个错误:

AssertionError [ERR_ASSERTION]:null =='MongoError:write EPIPE'

乍一看,我想到了这个SO解决scheme,并增加conf中的最大连接variables。 文件。 但是后来我想,这很愚蠢,我不需要这么多的连接,我应该能够限制这个数字,以免超载我的服务器,而且我实际上find了另一个SO解决scheme 。

所以我继续试图找出如何创build一个新的数据库每个variables的实例,因为这是我想要实现的,并在NodeJS驱动程序文档中find这个。 为MongoDB 。

我的代码基本上循环2个不同的数组。 首先: ccxt.exchanges (大约30项),然后pairs (4项),我创build一个新的连接每个数组的迭代…不知道如果我很清楚,但是这里的代码:

 var _ = require("lodash"); var ccxt = require("ccxt"); var MongoClient = require("mongodb").MongoClient, assert = require("assert"); let pairs = ["ETH/EUR", "BTC/EUR", "LTC/EUR", "BCH/EUR"]; let delay = 1200; // NOTE: Server initiation: setInterval(function() { ccxt.exchanges.forEach(r => { let exchange = new ccxt[r](); if (exchange.hasFetchOrderBook) { pairs.forEach(p => { exchange .fetchOrderBook(p) .then(async order => { if (_.isObject(order) && order.bids[0][1]) { let now = Math.floor(new Date()); order.mkt = r; order.pair = p; let mkmodel = r.charAt(0).toUpperCase() + r.slice(1) + "order"; var url = `mongodb://localhost:27017/${"order" + p.slice(0, 3)}`; MongoClient.connect(url, function(err, db) { assert.equal(null, err); var collection = db.collection(mkmodel); collection.insert(order, function(err, result) {}); db.close(); }); let compOrder = { mkt: order.mkt, pair: order.pair, aprice: order.asks[0][0], avol: order.asks[0][1], bprice: order.bids[0][0], bvol: order.bids[0][1], sn: order.timestamp, ping: now - order.timestamp, fees: exchange.fees }; var irl = `mongodb://localhost:27017/${"comp" + p.slice(0, 3)}`; MongoClient.connect(irl, function(err, db) { assert.equal(null, err); var collection = db.collection(mkmodel); collection.insert(compOrder, function(err, result) {}); db.close(); }); console.log(compOrder); await new Promise(resolve => setTimeout(resolve, delay)); } else { } }) .catch(e => {}); }); } }); }, 2000); 

为了简化我的代码,它是这样的:

 let array1 = [1, 2, 3, 4, 5, ..., 30]; let array2 = [1, 2, 3, 4]; array1.forEach(i => { array2.forEach(p => { new mongoConnection1 ==> Creates DB from Array2 (for instance: db = 1 on first iteration) Each Array1 item creates a Collection db.close(); someObjectManipulation new mongoConnection2 ==> Create different DB from Array2 as well Each Array1 item creates a Collection as well db.close(); }) }) 

现在,因为我的代码是asynchronous的(我宁愿保持这种方式)连接数太高,我得到的错误。 我想做这样的事情:

 let array1 = [1, 2, 3, 4, 5, ..., 30]; let array2 = [1, 2, 3, 4]; new mongoConnection array1.forEach(i => { array2.forEach(p => { Create DB from Array2 (for instance: db = 1 on first iteration) Each Array1 item creates a Collection someObjectManipulation Create different DB from Array2 as well Each Array1 item creates a Collection as well }) }) db.close(); 

为了限制连接的数量为1 …但我不确定是否有可能呢? 如果是这样,我想我将不得不在我的循环中的某个地方使用new Db(databaseName, topology, options) ( 如节点驱动程序文档中所示 ),但我不知道该怎么做…

我希望我的问题是清楚的,否则请让我知道在评论中,我会尽量澄清尽可能多的需要! 在此先感谢您的帮助人员!


编辑:

为了正确分类和处理我的数据,我希望为数组中声明的每一对数据库。 显然,我可以重新编写我的应用程序,以便它不是必需的,并且有一个数据库有许多集合,并且每个文档中的对具有一个键,但是数据会变得更加混乱。

感谢@Alistair_Nelson为了精简这个部分不清楚:)