Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

328
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda