简体   繁体   English

我们如何将超过 200 万条记录的大量数据从 mongo 表达发送到节点 js

[英]How we send in response large amount of data over 2 millions records from mongo expres to node js

i have a post api in express that response a mongo collection data that has over 2 milions records how we have to send back in response.我有一个帖子 api 表示响应 mongo 收集数据,其中包含超过 200 万条记录我们必须如何发回响应。 frontend: jquery ajax call backend: express mongo node前端:jquery ajax 调用后端:express mongo node

The problem arises because you are using more memory than you have.出现问题是因为您使用的 memory 比您拥有的多。 That's happens because you seem to be loading the 2 million records in memory then trying to JSON them before sending.发生这种情况是因为您似乎在 memory 中加载了 200 万条记录,然后在发送之前尝试 JSON 它们。

You will need to stream the mongo records to the response, that way you will have only a subset of the collection at a time.您将需要 stream 将 mongo 记录到响应中,这样您一次将只有一个集合的子集。

cursor.stream().pipe(JSONStream.stringify()).pipe(res);

JSONStream is a package that can help you do that very easily. JSONStream 是一个 package 可以帮助您非常轻松地做到这一点。 Cursor would be your mongo cursor. Cursor 将是您的 mongo cursor。

Read more about stream on the nodeJS docs to understand how it works.在 nodeJS 文档上阅读有关 stream 的更多信息,以了解它是如何工作的。 Basically you're receiving one document, stringify it, and send it out, rince and repeat.基本上你会收到一份文件,把它串起来,然后发送出去,然后重复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM