Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!