node.js - 如何在文本索引Mongodb中将精确短语与动态字符串匹配?
问题描述
我有这个查询
db.words.find({ "$text": { "$search": "\"cake sale\"" } }) // gives expected answer in robo3T
现在我的text
搜索是动态的
const text = "cake sale"
db.words.find({ "$text": { "$search": `\\"${text}\\"` } })
但它并没有给我预期的 nodejs 输出。那么如何在这里解析反斜杠呢?
解决方案
您的查询不正确。您需要更改查询
db.words.find({ "$text": { "$search": `"\"${text}\"` } })
至
db.words.find({ "$text": { "$search": `\"${text}\"` } })
因为,在第一个反引号之后的开头有一个额外的双引号 ( "
)。这样做将修复您的查询。
简单说明:
console.log("\"cake sale\"");
var text = "cake sale";
console.log(`\"${text}\"`);
// both the console.log gives same result
推荐阅读
- sql - Oracle 提取 json 字段
- javascript - 如何为 Table 控件中的不同实体使用绑定?
- python - 使用 SQL Alchemy 和 pandas read_sql 避免内存问题
- typescript - 理解带有回调类型的 React Native
- python - 具有修复资源的多处理
- java - (已解决)使用 Files.copy API 复制的文件在库中不可见
- r - 在 R 中可视化几列的正确方法是什么?
- react-native - React Native Navigation 导航未从 useEffect 触发
- python - 根据前两个字母删除分类变量
- mysql - 使用多语言站点(polylang)搜索和替换查询