执行多个Mongoose查询

时间:2013-12-20 17:37:52

标签: node.js mongodb mongoose database

我正在使用node和mongoose对我的mongodb运行查询。我有一组3个查询,我正在运行如下:

company.find({ 'shortName': eval("/" + req.params.query + "/i"), logoFileName : {$exists : true} }, function(err, b){
        if(err){
            console.log('brand query not found! ' + err);
            res.send(500, "Something broke!")
        }
        else{
            console.log("length of b : " + b.length)
            if(b.length>1){
                res.render('index', {
                    potentialBrands : b
                })
            }
            else{
                var brandResults  = b[0];   


            var industryQuery = company.find({GICSIndName: eval("'"+brandResults.GICSIndName+"'")}).sort({marketCap: -1}).limit(10);

            industryQuery.exec(function(err, industry){
                if(err){
                    console.log("There was an error! : " + err)
                    res.send(500, "Something broke!")
                }
                //if the colors have yet to be defined
                if(typeof brandResults.associatedColors[0] !== 'undefined'){
                    var colorQuery = company.find({'associatedColors.colorFamily': eval("'" + brandResults.associatedColors[0].colorFamily + "'") });

                    colorQuery.exec(function(err, colors){
                        if(err){
                            console.log("There was an error! : " + err)
                            res.send(500, "Something broke!")
                        }
                        console.log(colors);
                        res.render('brand',{
                            brandResult : brandResults,
                            industryResult: industry,
                            colorResult: colors,
                            queryName : req.params.query
                        });
                    })
                }
                else{
                    res.send(500, "Something broke!")
                }
            })

我目前的结构似乎效率很低,我想知道mongo或mongoose中是否有用于处理此类查询的内容。

3 个答案:

答案 0 :(得分:2)

我建议看看Promises。

http://mongoosejs.com/docs/api.html#promise_Promise

您的2个查询似乎也可以运行异步,您可以使用Q一起运行它们:

https://npmjs.org/package/mongoose-q

答案 1 :(得分:0)

出于组织目的(即,使代码更具可读性),有asyncstreamline等选项。

但是要解决有关效率的问题,您可以将其卸载到mongo,但不幸的是,能够根据文档中的值进行查询,您必须依赖mongo aggregation framework或{{ 3}},这可能是过度的,因为它们并不完全简单。

答案 2 :(得分:0)

只需使用异步

async.waterfall([

    function(wcallback){

      return wcallback(null, req.params.query);

    },

    function(query, wcallback){

       company.find({ 'shortName': eval("/" + req.params.query + "/i"), logoFileName : {$exists : true} }, function(err, b){
         if(err || !b || b.length == 0) return wcallback(err || true);
         else if(b.length == 1) return wcallback(null, b[0]);
         else....

    },

    function(brandResults, wcallback){

        company.find({'associatedColors.colorFamily': eval("'" + brandResults.associatedColors[0].colorFamily + "'") }, function(err, b){
           .....
           return wcallback(null, ...);
    }

  ], function(err, result){

     // final callback
     if(err) ... else ...

  });
相关问题