节点服务器在超过1000个项目的对象上崩溃 [英] node server crashes on object with over 1000 items

查看:244
本文介绍了节点服务器在超过1000个项目的对象上崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个集合超过1000个项目(1000个以下的项目是确定),这导致以下错误和崩溃时通过ajax调用请求:
(节点)警告:检测到递归process.nextTick。这将在下一版本的节点中断。请使用setImmediate进行递归推迟。

I have a collection with over 1000 items (Under 1000 items is ok.) which causes the following error and crash when requested via ajax call: (node) warning: Recursive process.nextTick detected. This will break in the next version of node. Please use setImmediate for recursive deferral.

这不会发生在本地开发。 (实际上与本地dev无关。
我在本地机器上运行了一个v0.10.0之前的版本,这个问题是10.0之后的版本)

This does not occur on local development. (actually has nothing to do with local dev. I was running a pre v0.10.0 on local machine and this issue is post 10.0)

节点v0.10.8与mongodb数据库和mongoose。

I am running node v0.10.8 with mongodb database and mongoose.

但是什么是最好的解决方案?

But what is best solution?

/ p>

See code:

 Collection.find().select('_id', 'first_name').sort('startTime', -1).exec(function(err,                 

  docs){

            var data = {'aaData' : docs};
            res.send(data);

        });


推荐答案

使用Mongoose对查询结果的流式传输支持,而不是在一个大数组中获取。

With that many docs in a result set, you should use Mongoose's support for streaming the result of the query rather than getting it in one big array.

var stream = Collection.find()
    .select('_id', 'first_name')
    .sort('startTime', -1)
    .stream();

stream.on('data', function (doc) {
  // do something with the doc like res.write(doc);
}).on('error', function (err) {
  // handle the error
}).on('close', function () {
  // the stream is closed, so complete the response with res.end();
});

这篇关于节点服务器在超过1000个项目的对象上崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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