Working on a search bar to search through a list of items (items have name and address) in react with mongo
the keys are called 'name' and 'Address'
I want the search bar to to display based on user input, it could be name or address based on regex
But I can only get the query to use one of the keys and not both
queryData['name'] = { $regex: searchField.replace(/[^a-zA-Z0-9-_@.]/g,''), $options: 'i' } // this lets me search through name array
When I change 'name' to address, it will use the address array for searchbar how can I use both name and address?
I tried doing
queryData['$or'] = [
{ name: {regex: searchField.replace(...), $options: 'i' }},
{ address: {regex: searchField.replace(...),$options:'i'}}
]
But the above method does not work, no errors on inspector, is my syntax off ? What else can I try? Thanks in advance.
The only thing is that in your syntax you don't have the $ in front of your regex strings. I tried your example on a MongoDB database that I have running locally and it seems to work as long as you use $regex, not regex, so (in my case):
{$or: [{name: {regex: "Energy"}},{category: {regex: "Values"}}]}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.