1

So, I want to add a search bar to my web application and I want to display the users when I enter names inside my input element.

I filter out the names on the back-end by creating a mongodb query and at the $regex I enter the string that the user entered in the input element (search bar) on the front-end. I thought it was working fine, but now my boss says that many users simply aren't showing up when he enters their name. I went through the entire thing and it has gotta have something to do with the regex I enter in the MongoDb query.

For example when I enter 'jonas' in the search bar I would expect that this would return the document of the user with the full_name property value Jonas Rodrigo. Am I wrong to think I can simply enter the input element string like I did below? And if so, how can I improve it?

So this is what it looks like: when I enter the name Jonas I expect this user to show, but unfortunately he doesn't show up.

enter image description here

this is my mongoDB query function: I simply enter the string ('jonas' in this case) into the $regex, but I must be doing something wrong because it doesn't return the desired user (the other two conditions are truthy)

const createUserQuery = (user, query) => User.find({
  $and: [
    { full_name: { $regex: query, $options: 'i' } },
    { _workspace: user._workspace },
    { active: true }
  ]
}).select('profile_pic full_name email created_date');

example mongodb user document

enter image description here

tilly
  • 2,229
  • 9
  • 34
  • 64

2 Answers2

1

The problem may be with your regular expression query.

If you want to search for all names starts with Jonas

The value of the query parameter you pass to the createUserQuery should be like

^Jonas

^ is the key here to get all names start with Joans

Mani
  • 1,471
  • 1
  • 13
  • 19
  • Well, Jonas is just an example. I also want users to enter the last name and match that in the full_name property as well. – tilly Mar 05 '19 at 18:12
  • 1
    @tilly what I am trying to say is you should form your regular expression like that .. that is just an example. As I asked earlier what is the value you are passing for query variable? – Mani Mar 05 '19 at 18:14
  • I see. So what I know basically add is the string 'jonas' as the regexp. Basically the string I enter in the input element on the front-end is what I'll end up putting as the regexp – tilly Mar 05 '19 at 18:15
  • In this case it will search for the exact string as 'joans'. Adding `^` symbol at start will check for all names starts with joans – Mani Mar 05 '19 at 18:25
0

As @Mani has said, perhaps it's an issue with your RegEx. You could try something like this expression against your query: const regex = new RegExp(query, 'g').

For example, if query = Jonas, the RegEx should match documents with a full_name property containing "Jonas Rodrigo".

const regex = new RegExp(query, 'g');

const createUserQuery = (user, query) => User.find({
  $and: [
    { full_name: { $regex: regex, $options: 'i' } },
    { _workspace: user._workspace },
    { active: true }
  ]
}).select('profile_pic full_name email created_date');
Mike Hamilton
  • 1,519
  • 16
  • 24