Search code examples
node.jsmongodbmongooseblockingnonblocking

How to get large data via mongoose non-blocking?


how do I get a large collection via mongoose a way, that I get every document returned, not a huge array with the whole collection?

At the moment I am just using following query:

var query = templateData.find({});
query.exec(function (err, docs) {
    // docs as array
});

This way, the query function is something like blocking IO and not non-blocking. Is there a way to make this more non-blocking?


Solution

  • Well, since I took another look at the mongoose documentation after posting this question, I got over the stream() function, which fullfills non-blocking operations perfectly.

    Blame me, but I think it could be mentioned a bit more offensive in the mongoose documentation: http://mongoosejs.com/docs/api.html#query_Query-stream

    var query = templateData.find({}).stream();
    query.on('data', function (doc) {
        // do something with the mongoose document
    }).on('error', function (err) {
        // handle the error
    }).on('close', function () {
        // the stream is closed
    });