Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To to do a parallel "Query" in Mongoose

I am pretty new to Mongoose so please bear with me.

Is there a way to perform two queries in "parallel". Or at least query two documents and return their results together? The callback notation is tripping me up a little with the sync.

In pseudo code this is what I am looking for:

function someWork(callback) {   
    var task1 = service.doQueryAndReturnTask();   
    var task2 = service.doQueryAndReturnTask();

    waitAll(task1, task2);

    callback(task1, task2); 
}

I know this is not the solution, due to the need to have callback on the doQueryAndReturnTask, but I need a pattern that works and referrable doesnt chain the callbacks

like image 998
Slappy Avatar asked Dec 05 '22 10:12

Slappy


1 Answers

It's not about Mongoose. Node.js is an asynchronous language, so it allows you to execute any number of async tasks (e.g. querying a database) at the same time.

What you need is some lib to handle asynchronous control flow, like async.js or when.js:

var when = require('when');

var someWork = function(callback) {
  when.all([
    collection1.find(query1).exec(),
    collection2.find(query2).exec()
  ]).spread(callback)
    .otherwise(function(err) {
      // something went wrong
    });
};

when.js is a module to handle promises. So, if you don't need promises, you may use async.js instead:

var async = require('async');

var someWork = function(callback) {
  async.parallel([
    function(cb) { collection1.find(query1, cb) },
    function(cb) { collection2.find(query2, cb) }
  ], function(err, res) {
    if (!err) return callback.apply(null, data);
    // something went wrong
  });
};

Update: Promises are the alternative way to handle asynchronous control flow by wrapping asynchronous functions with promises.

Usually, to get the results of some asynchronous function you should pass it some callback which will be executed somewhere in the future.

When you're using promises, instead of passing some callback you're immediately getting the promise of the results of the executions which will be resolved somewhere in the future.

So, promises allows you to work with asynchronous functions in a synchronous way using promises instead of the real data. Promises also allows you to wait for the results at any point of the execution.

In my example I'm executing two queries getting two promises for their results. Then I'm telling node to wait until both promises are fulfilled passing their results to the callback function afterwards.

You can read promises/A+ specification here. You may also look at when.js api docs.

like image 54
Leonid Beschastny Avatar answered Jan 01 '23 21:01

Leonid Beschastny