使用节点js从mongodb下载大量数据

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

我想使用nodejs从mongodb数据库下载大量数据。但它失败了,如何解决?请帮忙。

节点服务:

function getDwl (req, res) {
  var queryObject = url.parse(req.url, true).query
  db.collection('history').
    find({ 'timestamp': { '$gte': queryObject.fromdate, '$lt': queryObject.todate } }, { 'type': 1, 'value': 1 }).
    toArray(function (err, result) {
      var index = 0, resultset = []
      var count = result.length
      if (count == 0) {
        res.writeHead(200, {
          'Content-Type': 'application/json',
        })
        res.write('')
        res.end()
      } else {
        result.forEach(function (doc) {
          if (doc != null) {
            var valdata = doc.alarms
            var fields = []
            var queryString = 'SELECT field1 FROM details c inner join ldetails l on c.loc_id=l.loc_id where no=\'' + doc.no + '\';'
            var dtfield1 = null
            connection.query(queryString, function (err, result) {
              index++
              console.log('result query')
              if (err) {
                console.log('err', err)
              } else {
                if (result.length > 0) {
                  dtfield1 = result[ 0 ].datafield1

                  if (dtfield1 != null) {
                    for (var x in valdata) {
                      resultset.push({ 'Name': dtfield1 })
                    }
                  }
                }

                if (index == count) {
                  console.log('result data')

                  res.writeHead(200, {
                    'Content-Type': 'application/json',
                  })
                  var resultData = resultset
                  res.write(JSON.stringify(resultData))
                  res.end()
                }
              }
            })
          }
        })
      }
    })
}

如何从MONGODB数据库的http请求中获取nodejs服务调用中的大量数据。

javascript node.js mongodb
1个回答
0
投票

您可以批量使用分页和下载数据。这将减少执行此操作所需的内存和CPU数量,并随着时间的推移进行分配。您可以使用node.js stream批量发送这些数据,如下所示:

const batchSize = 10000;
db.collection("history").find(query).count()
    .then(count => {
        const operations = [];
        for (let i = 0; i < count; i += batchSize) {
            operations.push(
                db.collection("history")
                    .find(query)
                    .skip(i)
                    .limit(i + batchSize)
                    .then(batchHandler) // batchHandler will contain your stream logic
            )
        }
        return Promise.all(operations);
    })
    .then(() => console.log("Everything is handled"));

或者,如果您可以控制客户端应用程序,则可以使用客户端请求参数简单地对集合进行分页。例如:

// http://example.com/get-huge-collection?skip=0&limit=10000

const hugeCollectionController = (req, res) => {
    const { skip = 0, limit = 10000 } = req.query;
    return db.getCollection("history")
        .find(query)
        .skip(+skip)
        .limit(+limit)
        .then(collection => {
            res.write(JSON.stringify(collection));
            res.end();
        })
}
© www.soinside.com 2019 - 2024. All rights reserved.