MongoDB / NodeJS:每个变量的最大连接数/数据库创建数

时间:2017-12-05 12:56:10

标签: node.js mongodb loops database-connection

我需要你帮助mongodb允许的最大连接数。我收到了这个错误:

  

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

乍一看,我想到了这个SO solution,并在conf中增加了最大连接变量。文件。 但后来我想,这很愚蠢,我真的不需要这么多连接,我应该能够限制数量不要超载我的服务器,我实际上找到了另一个SO solution朝那个方向发展。

所以我继续试图找出如何为每个变量实例创建一个新数据库,因为这是我想要实现的目标found this in the NodeJS driver doc. for MongoDB

我的代码基本上循环遍历2个不同的数组。首先:ccxt.exchanges(大约30项)然后pairs(4项)我在两个阵列的每次迭代中创建一个新连接...不确定我是否清楚但这里是代码:

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();
  })
})

既然我的代码是异步的(我宁愿保持这种方式),连接数太高而且我得到了错误。 我想做那样的事情:

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)as presented in the Node driver docs),但我不知道该怎么做......

我希望我的问题很清楚,否则请在评论中告诉我,我会尽可能地澄清!在此先感谢您的帮助!

修改

为了正确分类和处理我的数据,我想为数组中规定的每对配备一个数据库。显然,我可以重新编写我的应用程序,以便不需要它,并且有一个数据库包含许多集合,并且每个文档中的对都有一个键,但数据会变得更加混乱。

感谢@Alistair_Nelson准备这部分并不清楚:)

0 个答案:

没有答案