Hello I'm implementing a searching functionality in mongodb node js / react native ! I want whenever I type a letter in the searching input I want the backend to fetch documents that has that letter or string in there name ! I can do it the javascript way by fetching all the products then doing it with js but that wouldn't be a good user friendly ! so any suggestion ! make be a query like $contains or something
CodePudding user response:
you can use regex in your find query to achieve this functionality
To find documents that starts with a letter(s)
db.collection.find( { name: { $regex: /^test/i } } )
This will fetch all the documents where the name starts with test
To find documents that contains letter(s)
db.collection.find( { name: { $regex: /test/i } } )
Also you can remove i from the end if you want case sensitive results.