Search code examples
mongodbnode.jsstreaming

How to stream MongoDB Query Results with nodejs?


I have been searching for an example of how I can stream the result of a MongoDB query to a nodejs client. All solutions I have found so far seem to read the query result at once and then send the result back to the server.

Instead, I would (obviously) like to supply a callback to the query method and have MongoDB call that when the next chunk of the result set is available.

I have been looking at mongoose - should I probably use a different driver?

Jan


Solution

  • Streaming in Mongoose became available in version 2.4.0 which appeared three months after you've posted this question:

    Model.where('created').gte(twoWeeksAgo).stream().pipe(writeStream);
    

    More elaborated examples can be found on their documentation page.