簡體   English   中英

執行多個Mongoose查詢

[英]Executing multiple Mongoose queries

我正在使用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中是否有為處理此類查詢而構建的內容。

我建議看看Promises。

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

您的2個查詢似乎也可以運行異步,您可以使用Q一起運行它們:

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

出於組織目的(即,使代碼更具可讀性),有asyncstreamline等選項。

但是要解決有關效率的問題,你可以將其卸載到mongo,但不幸的是,能夠執行基於文檔中的值查詢等操作,你必須依賴mongo 聚合框架map-reduce ,這可能是矯枉過正因為它們並不簡單。

只需使用異步

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 ...

  });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM