Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 894
Jan Algermissen Avatar asked Sep 10 '11 15:09

Jan Algermissen


People also ask

Is MongoDB good for node JS?

Node and MongoDB work very-well together, in part because Mongo uses a JavaScript engine built into the database since JavaScript is good at handling JSON objects. Compared to other databases, such as MySQL, MongoDB is fast for storing certain types of data and can be automatically scaled.


1 Answers

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.

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)

like image 148
Dan Milon Avatar answered Sep 21 '22 11:09

Dan Milon