猫鼬:通过遍历一系列模型来查找数据 [英] mongoose : find data by looping on an array of models

查看:76
本文介绍了猫鼬:通过遍历一系列模型来查找数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我陷入了异步算法的困境:

i'm getting stuck on an asynchronous algorithm :

我有一系列的猫鼬模型:

I've an array of mongoose models :

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

我想获取每个集合的所有项目并将它们放入数组或类似的东西中. 如果执行此操作,则find回调的this关键字不会引用当前模型, 所以我无法知道哪些模型项属于

I'd like to grab all items of each collection and put them in an array or something like that. If I do that, the this keyword of the find callback doesn't refer to the current model, so impossible for me to know which model items belong to

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

  });

}

我还尝试了 async 库,因为我总是返回同一问题:无法知道哪个模型在回调内部执行find查询. 如果我使用诺言,请在then中表示同上.

I've also tried async library without success, because I always return to the same issue : impossible to know which model execute the find query inside the callback. Idem in a then if I use promises.

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

Please help me :) How would you do that ?

编辑 model.find调用query.find,query.find调用mquery.find.在mquery.find中,通过丢失该引用的时间来调用回调:this._collection.find(conds,options,utils.tick(callback)); /EDIT

EDIT model.find calls query.find, query.find calls mquery.find. In mquery.find, callback is called, by lost the this reference a that time : this._collection.find(conds, options, utils.tick(callback)); /EDIT

推荐答案

请检查此代码段,我为您提供了所需的工作样本. 请检查代码中的注释以更好地理解.

Please check this code snippet, I have made a working sample of what you need. Please check comments in the code for better understanding.

示例工作代码与您所需要的类似.另一个参考查询用于将异步与猫鼬一起使用.

Sample Working code similar to what you required. Another ref ques for using async with mongoose.

/*
 * 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
    ]
}

这篇关于猫鼬:通过遍历一系列模型来查找数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆