Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

137
Visualizações
MongoDB filter for secondary textbox with req.query.value undefined problem

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

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

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.

about 4 years ago · Juan Pablo Isaza Relatório

0

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!

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda