node.js - Node 服务器在超过 1000 个项目的对象上崩溃

标签 node.js mongodb crash mongoose

我有一个包含超过 1000 个项目的集合(1000 个项目以下是可以的。),当通过 ajax 调用请求时,这会导致以下错误并崩溃: ( Node )警告:检测到递归 process.nextTick。这将在下一版本的 Node 中打破。请使用 setImmediate 进行递归延迟。

这不会发生在本地开发中。 (实际上与本地开发无关。 我在本地计算机上运行 v0.10.0 之前的版本,此问题是 10.0 后的问题)

我正在使用 mongodb 数据库和 mongoose 运行 Node v0.10.8。

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

查看代码:

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

  docs){

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

        });

最佳答案

结果集中有这么多文档,您应该使用 Mongoose 对 streaming 的支持查询的结果而不是将其放入一个大数组中。

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

关于node.js - Node 服务器在超过 1000 个项目的对象上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17689027/

相关文章:

当文件访问权限被拒绝时,Cordova 应用程序崩溃

html - 多WebRTC页面渲染

node.js - 了解 `nvm ls` 的输出。一切都安装正确了吗?

node.js - 如何使用node.js http服务器从mongodb返回大量行?

node.js - Parse Cloud code "first"查询数组返回不同的结果

iphone - 应用程序停顿-没有崩溃,没有错误

node.js - 将中间件添加到单个路由 Js Express 的最简洁方法

node.js - Tomcat 和 NodeJS(不同的 IP)使用相同的 SSL 证书

mongodb - 蒙戈斯错误 "mirrored config server connections are not supported"

ios - Xcode Organizer 崩溃 没有帐户记录?