Hi, you are trying to load to much data in memory at once.

Usually to do this, people should use “pagination”, which with mongo is just using “skip” and “limit” options. Here https://codeforgeek.com/server-side-pagination-using-node-and-mongo/ you have an example. You also have to manage the pagination in the client side, sending que actual page and maybe even the pageSize to the server.

Other option without pagination is using streams. You have to read all data from database and connect this input stream with the output stream in the response object.Here https://medium.com/yld-blog/using-streams-readable-writable-transform-streams-and-flow-control-d45c280b13f0 an article about streams. Basically (I think), when you have the ReadStream `const stream = DataModel.find({}).lean().cursor();` you can just stream.pipe(res) .

--

--

Software engineer with 15 year of experience. Lately focused in Node.js ecosystem and good practices.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ángel Cereijo

Ángel Cereijo

85 Followers

Software engineer with 15 year of experience. Lately focused in Node.js ecosystem and good practices.