微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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" }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。