user15494703
user15494703

Reputation:

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

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
    })
})

Upvotes: 2

Views: 43

Answers (2)

NeNaD
NeNaD

Reputation: 20304

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

Upvotes: 0

batman567
batman567

Reputation: 889

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

Upvotes: 0

Related Questions