Alternatives to MongoDB cursor.toArray() in node.js

前端 未结 1 541
时光取名叫无心
时光取名叫无心 2021-01-19 06:01

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


        
1条回答
  •  一生所求
    2021-01-19 06:33

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

    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.

    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.

    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).

    0 讨论(0)
提交回复
热议问题