as from the MongoDB documentation,
db.articles.find( { $text: { $search: "\"coffee shop\"" } } )
would need to insert its escape characters in the collection.find into a variable named query belonging to req.query.name, so, that text index can be used.
var query = req.query.name;
collection.find({ $text: { $search: '"\"' + query + '\""'}})
I also tried
'"\"query\""'
unfortunately what I expected does not work, do you have any solution? thanks
One way to do this is using ES6 template literals . Try this:
var query = `"${req.query.name}"`;
collection.find({ $text: { $search: query}})
OR
var query = '"' + req.query.name + '"';
collection.find({ $text: { $search: query}})
Try this, this will help to make the same query as it is mentioned in Mongodb documentation. and it will use your index as well.
var value = `\\"${query}\\"`;
collection.find({ $text: { $search: value}})
and if by any chance, your outer double quotes are missing, then try this:
var value = `"\\"${query}\\""`
collection.find({ $text: { $search: value}})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.