user1620696 user1620696 - 1 year ago 46
Node.js Question

Is there a simple way to open a readable stream from Mongo in Node?

Using MySQL it is very easy to open a readable stream from the database. All we need to do is:

function getTableStream() {
connection.query('SELECT * FROM someTable WHERE field = value').stream();

After that we can do things like:

getTableStream.on('data', function(data) {

And this is blazing fast. In fact I tried this with something like 85 thousands of rows and it was able to list all the data on the console in less than one minute.

I'm wondering: is it possible to do the same thing with MongoDB? Can we open a readable stream from mongo and get the data from there via streaming?

If it is possible, how is it done with Mongo?

Answer Source

Call .stream() on a native driver cursor to get a readable stream:

var stream = collection.find().stream();
stream.on('data', function(doc) {
stream.on('error', function(err) {
stream.on('end', function() {
    console.log('All done!');