0

enter image description hereWhat I'm trying is fetching columns from the user to which the user entered, The Good thing is Columns are getting fetched and the bad thing is the columns that I had applied conditions are not working.Can anyone help me out? I want a query that works for the filter option as in many websites to filter any product or something. Newbie here!!!

routes.post('/FilterCandidate',function(req,res){
   var fetchparameter={};
   var dynamicquery={author : req.user.username};
   if(req.user.username){
   if(req.body.ConsultantName){
       fetchparameter["ConsultantName"] =  req.body.ConsultantName;
     }
   if(req.body.Location){
      fetchparameter["Location"] =  req.body.Location;
     }
   if(req.body.JobRole){
  fetchparameter["JobRole"] =  req.body.JobRole;
     }
   if(req.body.Skills){
      fetchparameter["Skills"] =  req.body.Skills.split(',');
     }
   if(req.body.VisaStatus){
      fetchparameter["VisaStatus"] =  req.body.VisaStatus;
     } 
   if(req.body.BillingRate){
      fetchparameter["BillingRate"] =  req.body.BillingRate;
     }
   if(req.body.experience){
      fetchparameter["experience"] = req.body.experience;
     }
   if(req.body.jobtype){
      fetchparameter["jobtype"] = req.body.jobtype;
     }
   if(req.body.Availability){
      fetchparameter["Availability"] = req.body.Availability;
    }
   if(req.body.experience){
      fetchparameter["Salary"] = req.body.Salary;
   }
   if(req.body.Salarytype){
      fetchparameter["Salarytype"] = req.body.Salarytype;
   }
    }


 /* This below code for conditions is not working*/

for(var key in fetchparameter){
   if (key== "Salary" ){
     dynamicquery[key] =  {$gte :fetchparameter[key]};
          }
         if(key == "Skills"){
             dynamicquery [key] = {$in : fetchparameter[key]};
          }
         if(key == "experience"){
            dynamicquery[key] = {$gte :fetchparameter[key]};
          }
         else{
             dynamicquery[key] = fetchparameter[key];
             }
   } 
 console.log(dynamicquery);
    Candidate.aggregate([ {$match : dynamicquery }],(err,docs) =>{
       res.render('FilteredCandidate',{'Candidates' : docs});
       });
 });

This is what I'm getting output to refer to the attached image

1
  • Please enter your error message as text so that others searching for your problem in the future can find your question on Google. Commented May 5, 2020 at 0:42

1 Answer 1

1

i think you should make a match object array it will make your code more neat and hoping that it will solve your problem

Ex:

var match = {$and:[]};
if(any condition satisfy){
match.$and.push({}) //condition
}

and most important do check if match.$and array must not be empty if it is then delete match.$and. this procedure will help you to maintain your query better provides more flexibility.

Sign up to request clarification or add additional context in comments.

3 Comments

actually, when it is checking for the condition, the condition is treated as an object, please refer to the attached screenshot of the output.
because you made it a key value pair which is type object so above explanation wil solve your problem even if you want me give a proper example i can covert your query into the suggested one
hey if it worked for you then either mark the answer accepted or edit the question and add tag solved in it might help others as well!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.