Leo Polanco
Leo Polanco

Reputation: 143

Search for a phrase using mongoose, express

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

Answers (1)

SuleymanSah
SuleymanSah

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

Related Questions