使用异步并行进行多个猫鼬调用

vrg*_*ost 1 asynchronous mongoose node.js

一直为此苦苦挣扎,而且我似乎缺少了一些显而易见的东西。举个例子。可以说我想同时打三个电话。为了使它成为一个简单的示例或易于测试,可以说我们想要一个脚本,该脚本从mongodb中询问以下信息。

我们想找到以下信息:-我们要列出每个索引的ns-我们要列出具有_id的用户-我们要列出每个有键的ns的键(是的,您说得对,这是一个荒谬的查询集,但它们在每个mongo数据库上都返回一些内容。)

因此,可以在mongo db上执行此操作的简单脚本是:

var mongoose = require("mongoose");
var morgan = require('morgan');             // log requests to the console (express4)
var async = require('async');
mongoose.connect('mongodb://localhost/admin');



var Indexes = mongoose.model('system.indexes', {
    name: String,
    ns: String
});
var Users = mongoose.model('system.users', {
    user: String
})


        Indexes.find().exec(function(err, docs){
            for( var doc in docs ){
                console.log("Got Index " + docs[doc].ns);
            }
        })
        Users.find({ _id: {$ne: null}}).exec(function(err, docs){
            for (var doc in docs){
                console.log("And user " + docs[doc].user );
            }
        })
        Indexes.find({ key : {$ne : null}}).exec(function(err, docs){
            for (var doc in docs) {
                console.log("And Index with a key " + docs[doc].ns);
            }
        })
Run Code Online (Sandbox Code Playgroud)

但是如果我想使用异步,我将如何处理。因此,尝试以下操作以并行运行它们:

var mongoose = require("mongoose");
var morgan = require('morgan');             // log requests to the console (express4)
var async = require('async');
mongoose.connect('mongodb://localhost/admin');



var Indexes = mongoose.model('system.indexes', {
    name: String,
    ns: String
});
var Users = mongoose.model('system.users', {
    user: String
})


/// So lets do it with async Map instead
var queries = [];
queries.push(Indexes.find().exec());
queries.push(Users.find({ _id: {$ne: null}}).exec());
queries.push(Indexes.find({ key : {$ne : null}}).exec());

async.parallel(queries, function(err, docs){
    console.log("I've done some stuff");
})
Run Code Online (Sandbox Code Playgroud)

这给我一个错误声明

task(_restParam(function(err,args){^ TypeError:对象不是函数

但是有没有解决此问题的简单方法。如何使用异步映射或并行进行多个查找。

=====继Jigars出色的回答之后,我们离答案更近了===== 现在,jigars出色的回答开始了,将内容保留在下面如果有人有相同的问题

/// So lets do it with async Map instead
var queries = [];
queries.push(function (cb) {
    try {
        result = Indexes.find().exec();
        cb(null, result);
    } catch(e) {
        cb(e);
    }
})

queries.push(function (cb) {
    try {
        result = Users.find({ _id: {$ne: null}}).exec();
        cb(null, result);
    } catch(e) {
        cb(e);
    }
})

queries.push(function (cb) {
    try {
        result = Indexes.find({ key : {$ne : null}}).exec();
        cb(null, result);
    } catch(e) {
        cb(e);
    }
})

async.parallel(queries, function(err, docs) {
    // if any query fails
    if (err) {
        throw err;
    }

    var res1 = docs[0]; // result of queries[0]
    for (var opts in res1){
        console.log("In res1 we got " + res1[opts])
    }
    var res2 = docs[1]; // result of queries[1]
    for (var opts in res2){
        console.log("In res2 we got " + res2[opts])
    }
    var res3 = docs[2]; // result of queries[2]
    for (var opts in res3){
        console.log("In res3 we got " + res3[opts])
    }
})
Run Code Online (Sandbox Code Playgroud)

因此,并行处理现在可以工作了,只是不返回我想要的东西。res1,res2和res3中的结果类似于js代码:

Jig*_*ain 5

您的query.push命令应采用以下方式

queries.push(function (cb) {
    Indexes.find().exec(function (err, docs) {
        if (err) {
            throw cb(err);
        }

        // do some stuff with docs & pass or directly pass it
        cb(null, docs);
    });
})    

queries.push(function (cb) {
    Users.find({ _id: {$ne: null}}).exec(function (err, docs) {
        if (err) {
            throw cb(err);
        }

        // do some stuff with docs & pass or directly pass it
        cb(null, docs);
    });
})

queries.push(function (cb) {
    Indexes.find({ key : {$ne : null}}).exec(function (err, docs){
        if (err) {
            throw cb(err);
        }

        // do some stuff with docs & pass or directly pass it
        cb(null, docs);
    });
})

async.parallel(queries, function(err, docs) {
    // if any query fails
    if (err) {
        throw err;
    }

    var res1 = docs[0]; // result of queries[0]
    var res2 = docs[1]; // result of queries[1]
    var res3 = docs[2]; // result of queries[2]
})
Run Code Online (Sandbox Code Playgroud)

async使用多个函数,其中回调函数为参数。cb每个功能完成后,您需要调用一次。另外,您还需要传递每个查询的结果值。当所有查询完成并行执行时,将以数组形式返回给您