I'm using node and mongoose to run queries against my mongodb. I have a set of 3 queries that I'm running as follows :
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!")
}
})
My current structure seems rather inefficient and I was wondering if there is something within mongo or mongoose that is built for handling such queries.