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.
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