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

327
Visualizações
Finding words from two properties in a mongodb document

I want to find a word from two properties (columns) 'firstname' and 'lastname' from a document. The code below is not giving the response while finding from multiple properties. I am new to mongo db please make me aware of my mistake and its possible solution.

router.get('/search/:user', (req, res) => {
  const searchField = new RegExp(req.params.user, 'i');
  User.find({ firstname: { $regex: searchField }, lastname: { $regex: searchField } })
    .then((data) => {
      res.json(data);
    })
    .catch((error) => {
      console.log(error);
    });
});

It is working when I pass a single property though. How can I make it possible to find from both 'firstname' and 'lastname' at once.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Rohit Dalal's answer is the correct solution.

User.find({ $or: [{ firstname: { $regex: searchField } }, { lastname: { $regex: searchField } }] })

This is the desired solution.

over 4 years ago · Santiago Trujillo 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