node.js 中 MongoDB cursor.toArray() 的替代方案 [英] Alternatives to MongoDB cursor.toArray() in node.js

查看:110
本文介绍了node.js 中 MongoDB cursor.toArray() 的替代方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 MongoDB 游标的 toArray() 函数将数据库结果转换为数组:

I am currently using MongoDB cursor's toArray() function to convert the database results into an array:

run = true;
count = 0;
var start = process.hrtime();
db.collection.find({}, {limit: 2000}).toArray(function(err, docs){
  var diff = process.hrtime(start);
  run = false;
  socket.emit('result', {
    result: docs,
    time: diff[0] * 1000 + diff[1] / 1000000,
    ticks: count
  });
  if(err) console.log(err);
});

这个操作在我的电脑上大约需要 7ms.如果我删除 .toArray() 函数,则操作大约需要 0.15 毫秒.当然这行不通,因为我需要转发数据,但我想知道这个函数在做什么,因为它需要这么长时间?数据库中的每个文档仅由 4 个数字组成.

This operation takes about 7ms on my computer. If I remove the .toArray() function then the operation takes about 0.15ms. Of course this won't work because I need to forward the data, but I'm wondering what the function is doing since it takes so long? Each document in the database simply consists of 4 numbers.

最后,我希望在更小的处理器上运行它,比如树莓派,这里从数据库中获取 500 个文档并将其转换为数组的操作大约需要 230 毫秒.这对我来说似乎很多.还是我期望太高了?

In the end I'm hoping to run this on a much smaller processor, like a Raspberry Pi, and here the operation where it fetches 500 documents from the database and converts it to an array takes about 230ms. That seems like a lot to me. Or am I just expecting too much?

是否有其他方法可以在不使用 toArray() 的情况下从数据库中获取数据?

Are there any alternative ways to get data from the database without using toArray()?

我注意到的另一件事是,在获取数据库结果时,整个 Node 应用程序的速度明显变慢.我创建了一个简单的间隔函数,它应该每 1 毫秒增加一次计数值:

Another thing that I noticed is that the entire Node application slows remarkably down while getting the database results. I created a simple interval function that should increment the count value every 1 ms:

setInterval(function(){
  if(run) count++;
}, 1);

然后我希望计数值几乎与时间相同,但在我的计算机上 16 毫秒的时间内,计数值是 3 或 4.在 Raspberry Pi 上,计数值从未增加.是什么占用了如此多的 CPU 使用率?当被要求重复运行数据库查询时,监视器告诉我我的计算机使用了 27% 的 CPU,而 Raspberry Pi 使用了 92% 的 CPU 和 11% 的 RAM.

I would then expect the count value to be almost the same as the time, but for a time of 16 ms on my computer the count value was 3 or 4. On the Raspberry Pi the count value was never incremented. What is taking so much CPU usage? The monitor told me that my computer was using 27% CPU and the Raspberry Pi was using 92% CPU and 11% RAM, when asked to run the database query repeatedly.

我知道有很多问题.非常感谢任何帮助或解释.我还是 Node 和 MongoDB 的新手.

I know that was a lot of questions. Any help or explanations are much appreciated. I'm still new to Node and MongoDB.

推荐答案

db.collection.find() 返回一个游标,而不是结果,而且打开一个游标非常快.

db.collection.find() returns a cursor, not results, and opening a cursor is pretty fast.

一旦您开始读取游标(使用 .toArray() 或使用 .each().next()), 实际文件正在从数据库中传输给你的客户.该操作占用了大部分时间.

Once you start reading the cursor (using .toArray() or by traversing it using .each() or .next()), the actual documents are being transferred from the database to your client. That operation is taking up most of the time.

我怀疑使用 .each()/.next()(而不是 .toArray(),后者在幕后使用了这两者之一)将大大提高性能,但您可以随时尝试(谁知道).由于 .toArray() 会读取内存中的所有内容,这可能是值得的,尽管听起来您的数据集没有那么大.

I doubt that using .each()/.next() (instead of .toArray(), which—under the hood—uses one of those two) will improve the performance much, but you could always try (who knows). Since .toArray() will read everything in memory, it may be worthwhile, although it doesn't sound like your data set is that large.

我真的认为 Raspberry Pi(特别是 Model 1)上的 MongoDB 不会很好地工作.如果您不太依赖 MongoDB 查询功能,则应考虑使用替代数据存储.甚至可能是内存存储(500 个文档乘以 4 个数字听起来并不需要大量 RAM).

I really think that MongoDB on Raspberry Pi (esp a Model 1) is not going to work well. If you don't depend on the MongoDB query features too much, you should consider using an alternative data store. Perhaps even an in-memory storage (500 documents times 4 numbers doesn't sound like lots of RAM is required).

这篇关于node.js 中 MongoDB cursor.toArray() 的替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆