2

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!

2

1 Answer 1

1

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"]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.