M
M
Maxim2017-07-20 14:08:13
MongoDB
Maxim, 2017-07-20 14:08:13

How to get a large amount of data in batches in mongoose + nodejs?

There is a method in the controller that requests a large amount of data from the database through such a chain Model.find(obj).select(obj).populate(str).exec(fn)
. There is a lot of data and mongo falls on a timeout. I decided to look for how to receive data in batches. Stumbled upon a solution with paging using .skip()and .limit(). But I did not understand how to get all the data in this way. Send recursive requests with a change in the transmitted values ​​in .skip()and .limit()? Maybe there is a normal solution, how to get a data footcloth without overloading the server
Please help me, I'm new to mongo and node
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim, 2017-07-20
@hostnameempty

Everything turned out to be easy
Made with the helpcursor()

var bigData = [];
var cursor = Model.find(obj).select(obj).populate(str).cursor({batchSize:100});
cursor.
  on('data', doc => {
     if(doc) {
       bigData.push(doc);
     }
  })
  .on('end', () => {
     // производим дальнейшие действия с данными
  })

S
SergeyBugai, 2017-07-20
@SergeyBugai

I think this is what you need link

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question