0

i am trying to do the equivaleny in mongodb to sql LIKE search, but i cant have the results that i expected, i am follow this mappig sql to monog, but the result json is always empty. if i use a literal string works , but when pass a variable nothing happens

app.get("/questions/search/:query", function(req,res){
  var querySearch = req.params.query;
    //res.send(querySearch)

  Question.find({title: /querySearch/ },function(err,docs){
    if(err) res.json(err)
    res.json(docs)
  });
})
0

1 Answer 1

0

That find command will search for documents where the title contains the string "querySearch". This doesn't seem to be what you want to do. When you want to use find with a regular expression created at runtime, pass a RegExp object.

But please note that searching with regular expressions is slow. When you don't need all the features of regular expressions and only search for whole words, a text index might be the better choice.

Sign up to request clarification or add additional context in comments.

1 Comment

yes, with RegExp works fine, thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.