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

154
Vistas
How can I define argument type as a string from list of strings & run mongo db query

How can I define the arg type in a function as a string from list of strings and run a mongodb query. What I am doing is given below:

users.services.ts

async findOne(key: "_id" | "email" | "username", value: string) {
    
    const user = await this.userModel.findOne({ key: value }).exec();
    if (!user) {
      throw new NotFoundException();
    }
    return user;
  }

this is where & how I am calling findOne in users.services.ts

const user = await this.usersService.findOne("username", username);

Its not working and when debugging its not picking the values of key & value.
Many thanks for your help in advance.

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

0

You need to use $or query

{
 $or: [
     {"_id": value},
     {"email": value},
     {"username": value}
 ]
}

It's better to have three/more different functions and make a call with the given key than having list of or conditions.

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