Home > Mobile >  MongoDB filter for secondary textbox with req.query.value undefined problem
MongoDB filter for secondary textbox with req.query.value undefined problem

Time:12-01

i noticed with node, express and mongo driver if a req.query.x/y... is undefined it returns me invalid during a search! example:

//req.query.name
//req.query.email
//req.query.city

var search = req.query.search; //used in the main serch box
//used as secondary textbox filters
if(req.query.name){
 var name = req.query.name;
  }
 if(req.query.email){
 var email= req.query.email;
  }
 if(req.query.city){
 var city= req.query.city;
  }

during a search on multiple textboxes I would like to eliminate the if conditions as much as possible!

in .ejs

//textbox filter
<input type="text" name="name">
<input type="text" name="email">
<input type="text" name="city">

in .js

collection.find({
$text: {$search: search},
    or:[{
     name:{$regex: '^' name},
     email:{$regex: '^' email},
     city:{$regex: '^' city}}
       ]
 }).toArray(function.......

I simplified the code a lot to get to the point of the situation (but I don't think the problem concerns the $and or $or and their structure|position)! however if one of the search conditions is undefined I do not get the desired result, I would like to understand if possible to exclude city or name or email in case there are no assigned values! how could I do ? i can't use req.query because it matches a higher search box. in a nutshell these three boxes would be filtering from a text req.query.search

I tried that but without success:

if(name == undefined ){
name ="";}
if(email == undefined{
email="";}
if(city==undefined){
city="";}

thanks to anyone who answers me

CodePudding user response:

You can create your filter object and then pass to it your properties based if they're undefined or not; you can try it this way:

const {
  search,
  name,
  email,
  city,
} = req.query;

const filter = {
  $text: { $search: search },
  $or: [],
};
if (name) filter.$or.push({ name: new RegExp(name, 'i') }); // The 'i' flag is just for example purpose, that's all
if (name) filter.$or.push({ email: new RegExp(email, 'i') }); // same here
if (name) filter.$or.push({ city: new RegExp(city, 'i') }); // same here

const result = await collection.find(filter);

PS: Your using $text keyword, I don't know if you added a text index to your Mongoose schema definition that lets you use the $text operator in your find queries to search all fields included in the text index. You can read more about it here.

CodePudding user response:

Thanks for the advice, it would seem that I solved it this way (let's hope nothing escapes me)

var search = req.query.search; //used in the main serch box
    //used as secondary textbox filters
    if(req.query.name){
     var name = req.query.name;
      }
    else if(name === undefined){name ="";}\\add this
     if(req.query.email){
     var email= req.query.email;
      }
    else if(email === undefined){email="";}\\add this
    
     if(req.query.city){
     var city= req.query.city;
      }
    else if(city === undefined){city="";}\\add this

If there is a better way, so be it!

  • Related