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

265
Views
How to create search/autocomplete functionality using mongodb and expressjs?

So basically I want to create a search functionality for my users schema. I would take in a query for a user based on their name and return relevant users.

For example:

query = "leo", response = ["leonel messi", "leonardo dicaprio", ...]

I already tried using the text index but since that requires entire words to be inputted, it doesn't fit my constraints since a query will most likely be characters rather than entire words.

So essentially Im looking to make an autocomplete which returns relevant users based on some input from the user.

How could I accomplish this? Thank you!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

As shown by Dov Rine and rajabraza in the comments, I could simply use a regex to find make such an implementation.

Code:

const searchResults = await Users.find({ name: { $regex: `^${req.params.name}`, $options: 'i' } }).select(["name"]);
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!