I believe that for a "fuzzy" search you will need to use a regular expression. This should accomplish what you are looking for (source of the escapeRegex function here ):
function escapeRegex(text) { return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); }; router.get("/", function(req, res) { if (req.query.search) { const regex = new RegExp(escapeRegex(req.query.search), 'gi'); Jobs.find({ "name": regex }, function(err, foundjobs) { if(err) { console.log(err); } else { res.render("jobs/index", { jobs: foundjobs }); } }); } }
However, your application may experience performance issues when querying mongo for regular expression. Using a library, such as search-index for search, can help optimize the performance of your application, with the added benefit of searching for word stems (for example, returning “found” from “find”).
UPDATE: my initial answer included a simple regular expression that would leave your application vulnerable to a regular DDoS attack . I updated the "safe" escaped regex.
source share