How to stream MongoDB Query Results with nodejs?
node-mongodb-driver
(the underlying layer that every mongoDB client uses in nodejs) except the cursor API that others mentioned has a nice stream API (#458). Unfortunately i did not find it documented elsewhere.
Update: there are docs also here.
It can be used like this:
var stream = collection.find().stream()stream.on('error', function (err) { console.error(err)})stream.on('data', function (doc) { console.log(doc)})
It actually implements the ReadableStream interface, so it has all the goodies (pause/resume etc)
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.
mongoose
is not really "driver", it's actually an ORM wrapper around the MongoDB driver (node-mongodb-native
).
To do what you're doing, take a look at the driver's .find
and .each
method. Here's some code from the examples:
// Find all records. find() returns a cursorcollection.find(function(err, cursor) { sys.puts("Printing docs from Cursor Each") cursor.each(function(err, doc) { if(doc != null) sys.puts("Doc from Each " + sys.inspect(doc)); }) });
To stream the results, you're basically replacing that sys.puts
with your "stream" function. Not sure how you plan to stream the results. I think you can do response.write() + response.flush()
, but you may also want to checkout socket.io
.