How to get all results that contain a certain string (explained more in post)

Viewed 33

So, let's say I put "h" in a post form and I have 5 things in my data base:

1st Entry - Name: Hello

2nd Entry - Name: Height

3rd, 4th, 5th entries do not have a word that has "h" in them

How would I make it return all of the ones that include the string provided (like Hello and Height)

Here is my current setup:

app.post('/search/full', async (req, res) =>{
  const URLs = await shortURL.find({
      "short": {
        "$regex": req.body.fullURLSearch,
        "$options": "i"
      }
    })
    res.render('searchFull', {
        fullURLsearch: URLs
    })
})
2 Answers

You don't really give much to go on, but based on your question I can assume something like this might work:

db.collection.find({field:/H/}})

naturally replace the H with a variable as required

If you want case insensitive query, you can do this (if you want case sensitive query, just remove "$options": "i"):

db.collection.find({
  "field": {
     "$regex": "YourQueryString",
     "$options": "i"
  }
})

Here is the working snippet: https://mongoplayground.net/p/F6oSaf8P7lb

Related