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

153
Visualizações
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 Respostas
Responde à pergunta

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 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