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

216
Views
Filtering with an array of objects Regex in mongodb and mongoose

I'm trying to create a dynamic filter that can be implemented using at least 3 letters. I've got a lot of fields which i will be filtering against.

As an example, If I am trying to find users by email, I want to be able to type "@gma" or at least "gma" and ideally, it should return an array of all users containing the specified filtering value. This should be the same when searching for properties like firstName and so on

My current solution only works if I provide a full value that matches what I already have in my database. e.g test@gmail.com for email or john for firstName. I want to be able to type jo for the latter.

const regexPattern = new RegExp(["^", filterUsersByValue, "$"].join(""), "i");
const filteredU = UserModel.find({ [filterUsersBy]: regexPattern})
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If I've understood correctly, since you are using JS you can create the find object like this:

let findObj = {}
findObj[userKey] = {$regex:userFilter, $options:"i"}
const filteredU = UserModel.find(findObj)

This creates que object like this:

const userFilter = "gma"
const userKey = "email"
let findObj = {}
findObj[userKey] = {$regex:userFilter,$options:"i"}
console.log(findObj)

What is the same that in this query

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!