Reputation: 143
Following the documentation of mongodb here, I'm trying to search for something using mongoose while using the search query from express, my code is this
collection_name.find{$text: { $search: req.query.search} }
but this returns the wrong set of data from the db (since it isn't taking the query as a phrase)
I'm trying to take this
db.articles.find( { $text: { $search: "\"coffee shop\"" } } )
(which is the example from the documentation, and it was giving the results that I wanted)
into this:
collection_name.find( { $text: { $search: "\"req.query.search\"" } } )
but taking the req.query.search as a variable instead of a string.
Upvotes: 2
Views: 719
Reputation: 17858
I think you need to use backtick character to construct search term like this:
db.articles.find( { $text: { $search: `"${req.query.search}"` } } )
Upvotes: 3