问题描述
我在本地 MongoDB 上有这些数据,我想搜索包含“the”的 title
字段。
postsArray = await posts.find({ $text: { $search: "the" } }).toArray()
但是,如果我输入了确切的 title
,它会返回正确的结果
postsArray = await posts.find({ $text: { $search: "the game" } }).toArray()
知道为什么会这样吗?
谢谢
解决方法
$text
搜索不会匹配the 和and 词,有关于匹配操作的说明:
$text
运算符会忽略特定于语言的停用词,例如英语中的 the 和 and。
让我们举一个不同单词的例子,它应该可以工作:
假设我们有以下文档:
{ "title": "Good Morning" }
查询 1: 搜索“好”字:
{ $text: { $search: "good" } }
结果: Playground
{ "title": "Good Morning" }
查询 2: 搜索“早上好”字样:
{ $text: { $search: "good morning" } }
结果: Playground
{ "title": "Good Morning" }