svennergr
svennergr

Reputation: 810

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?

Upvotes: 2

Views: 5062

Answers (2)

Jared Rewerts
Jared Rewerts

Reputation: 131

It looks like the new standard may be to use cursors.

Upvotes: 2

svennergr
svennergr

Reputation: 810

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
});

Upvotes: 9

Related Questions