1

假设我有一个只有一个字段的集合BlogText。当用户搜索一个单词并且该单词出现在 中时BlogText,我想:

  1. 仅检索匹配词之前的 10 个词和匹配查询之后的 10 个词,以省略号开头并后跟。
  2. 另外,我想替换Matched word<b>Matched word</b>

例如,如果搜索到的查询是1500,我想检索以下内容:

... has been the industry's standard dummy text ever since the <b>1500<b>s, when an unknown printer took a galley of type and ...

鉴于原始文本BlogText

Lorem Ipsum 只是印刷和排版行业的虚拟文本。自 1500 年代以来,Lorem Ipsum 一直是行业的标准虚拟文本,当时一位不知名的印刷商采用了一种类型的厨房并将其加扰以制作一本类型样本书。它不仅经历了五个世纪,而且经历了电子排版的飞跃,基本保持不变。它在 1960 年代随着包含 Lorem Ipsum 段落的 Letraset 表的发布而流行起来,最近还随着 Aldus PageMaker 等桌面出版软件(包括 Lorem Ipsum 的版本)而普及。

我知道这也可以在服务器上完成,但我想避免检索我不需要的数据(参考第一点)。

4

1 回答 1

2

您可以使用聚合返回长文本的子字符串。

假设您需要一个围绕匹配项的第一次出现的子字符串,并且使用空格作为单词分隔符,管道可以是这样的:

db.collection.aggregate([
    { $match: { BlogText:/1500/ } },
    { $project: {
        match: {
            $let: {
                vars: { pos: { $indexOfCP: [ "$BlogText", "1500" ] }},
                in: { $concat: [
                    { $reduce: {
                        input: { $slice: [ 
                            { $split: [ 
                                { $substrCP: [ "$BlogText", 0, "$$pos" ] }, 
                                " " 
                            ]}, 
                            -10 
                        ]},
                        initialValue: "",
                        in: { $concat : [ "$$value", " ", "$$this" ] }
                    }},
                    { $reduce: {
                        input: { $slice: [ 
                            { $split: [ 
                                { $substrCP: [  "$BlogText", "$$pos", { $strLenCP: "$BlogText" } ] }, 
                                " " 
                            ]}, 
                            10 
                        ]},
                        initialValue: "",
                        in: { $concat : [ "$$value", " ", "$$this" ] }
                    }}            
                ]}
            }
        } 
    }}
]);
于 2017-08-30T11:16:18.953 回答