MongoDB $text $search 返回精确短语而不是包含

问题描述

I

我在本地 MongoDB 上有这些数据,我想搜索包含“the”的 title 字段。

当我使用这段代码时,它返回了一个空数组:

postsArray = await posts.find({ $text: { $search: "the" } }).toArray()

但是,如果我输入了确切的 title,它会返回正确的结果

postsArray = await posts.find({ $text: { $search: "the game" } }).toArray()

根据文档,第一个代码也应该返回相同的结果,

知道为什么会这样吗?

谢谢

解决方法

$text 搜索不会匹配theand 词,有关于匹配操作的说明:

Stop Words

$text 运算符会忽略特定于语言的停用词,例如英语中的 theand


让我们举一个不同单词的例子,它应该可以工作:

假设我们有以下文档:

{ "title": "Good Morning" }

查询 1: 搜索“好”字:

{ $text: { $search: "good" } }

结果: Playground

{ "title": "Good Morning" }

查询 2: 搜索“早上好”字样:

{ $text: { $search: "good morning" } }

结果: Playground

{ "title": "Good Morning" }