mongoose:通过在模型数组上循环来查找数据

我陷入了一个asynchronousalgorithm:

我有一系列的mongoose模型:

var allRefDatasSchemas = { RefAllotement: mongoose.model('RefAllotement', RefDataSchema), RefModeleConstructeur: mongoose.model('RefModeleConstructeur', RefDataSchema), RefTypeKit: mongoose.model('RefTypeKit', RefDataSchema), RefTypeUtilisation: mongoose.model('RefTypeUtilisation', RefDataSchema), }; 

我想抓住每个集合的所有项目,并把它们放在一个数组或类似的东西。 如果我这样做, this findcallback的关键字不会引用当前模型,所以我不可能知道哪些模型项属于

 var results = {}; for (var model in allRefDatasSchemas) { allRefDatasSchemas[model].find(function(err, data) { // I'd like to do something like that : // but this.modelName is null, because it isn't the model // on which the find is done. results[this.modelName] = data; // if I use "model" variable, it doesn't work, because asynchronous callback }); } 

我也试过asynchronous库没有成功,因为我总是回到相同的问题:不可能知道哪个模型在callback内执行find查询。 同样,如果我使用承诺。

请帮助我:)你会怎么做?

编辑model.find调用query.find,query.find调用mquery.find。 在mquery.find中,callback被调用,通过失去这个引用a:this._collection.find(conds,options,utils.tick(callback)); /编辑

请检查这个代码片段,我已经做了你需要的工作示例。 请在代码中查看注释以获得更好的理解。

示例工作代码类似于您所需的。 另一个参考是使用mongooseasynchronous。

 /* * Object to store all models */ var allRefDatasSchemas = { RefAllotement: mongoose.model('RefAllotement', RefDataSchema), RefModeleConstructeur: mongoose.model('RefModeleConstructeur', RefDataSchema), RefTypeKit: mongoose.model('RefTypeKit', RefDataSchema), RefTypeUtilisation: mongoose.model('RefTypeUtilisation', RefDataSchema), }; /* * need an array to run all queries one by one in a definite order using async waterfall mwthod */ var arr = []; for(each in allRefDatasSchemas) { arr.push(each); } /* * Callback function for initiation of waterfall */ var queue = [ function(callback) { // pass the ref array and run first query by passing starting index - 0 callback(null, arr, 0) } ]; /* * Object to store result of all queries */ var finalResult = {}; /* * Generic Callback function for every dynamic query */ var callbackFunc = function(prevModelData, currentIndex, callback) { allRefDatasSchemas[arr[currentIndex]].find(function(err, result) { if(err) { console.log(err) } else { // Your Query // // I'd like to do something like that : // but this.modelName is null, because it isn't the model // on which the find is done. // arr[currentIndex] will point to // RefAllotement, RefModeleConstructeur etc. as you required finalResult[arr[currentIndex]] = result // send current result to next interation if required or you can skip // and increment the currentIndex to call next query callback(null, result, currentIndex + 1) } }) } /* * Add callback function for every dynamic query */ for(each in allRefDatasSchemas) { queue.push(callbackFunc); } /* * Run all dynamic queries one by one using async.js waterfall method */ async.waterfall(queue, function (err, result) { // Final object with result of all the queries console.log('finish', finalResult) }); 

输出将以这种格式

 finish { RefAllotement:[ // Result of RefAllotement query ], RefModeleConstructeur:[ // Result of RefModeleConstructeur query ], RefTypeKit:[ // Result of RefTypeKit query ], RefTypeUtilisation:[ // Result of RefTypeUtilisation query ] }