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

180
Vistas
Ignore undefined values in the find method

I have a service function that should return users based on condition, when it's present, or return just all records from collection:

function getUsers(status: string[]) {
  return this.usersModel.find({ status });
}

But when users don't submit anything in params, the status will be undefined and mongo will search for users with status: undefined. Can I make the mongoose find function ignore status if it's undefined and do not include it in the query?

NOTE: I don't want to explicitly construct the query in the service, as it's violating the service scope of responsibility.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Perhaps I'm missing something here, but is there anything stopping you from simply running a different query (one absent of such conditions) if status is empty?

It is afterall a service function, business logic isn't the end of the world :)

about 4 years ago · Juan Pablo Isaza 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