且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MongoDB在聚合查询中使用$ text

更新时间:2022-11-26 18:25:50

您可以从 match 查询中删除对象"text" ,然后将在匹配查询中直接输入$ text运算符,如下所示:

You can remove the object "text" from the match query and put the $text operator straight away within the match query as follows:

db.notifications.aggregate([
    {
        $match: {
            $text: {
                $search: "faizul"
            }
        }
    }
])

在执行上述操作之前,请确保已为要文本搜索的字段设置了索引类型作为文本.

Before executing the above make sure that you had made the index type as text for the field that you want to text search for.

就我而言,我已将 agent_id 字段标记为 文本IndexType .

In my case I had marked agent_id field as text IndexType.

查询结果: 从查询中可以看到,我只是在文本搜索中输入了faizul,而不是完整的电子邮件地址.效果很好.

Query Result: From the query you could see that I had simply entered faizul in the text search and not the full email address. Which works fine.