我有这个查询
db.words.find({ "$text": { "$search": "\"cake sale\"" } }) // gives expected answer in robo3T
Now my text
搜索是动态的
const text = "cake sale"
db.words.find({ "$text": { "$search": `\\"${text}\\"` } })
但它没有给我 Nodejs 的预期输出。那么我该如何解析这里的反斜杠呢?
您的查询不正确。您需要更改查询
db.words.find({ "$text": { "$search": `"\"${text}\"` } })
to
db.words.find({ "$text": { "$search": `\"${text}\"` } })
因为,有一个额外的双引号 ("
) 在第一个反引号之后的开头。这样做将解决您的查询。
简单说明:
console.log("\"cake sale\"");
var text = "cake sale";
console.log(`\"${text}\"`);
// both the console.log gives same result
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)