用于计算mongodb中主字符串中子字符串出现次数的聚合
我是MongoDB的新手,可能是一个noob问题 我想计算消息字段中重复“lupoK”的次数,即“message”:“第一个lupoK lupoK”使用MongoDB中的聚合,我使用的是studio3t接口 我的文件结构是—用于计算mongodb中主字符串中子字符串出现次数的聚合,mongodb,aggregation,studio3t,Mongodb,Aggregation,Studio3t,我是MongoDB的新手,可能是一个noob问题 我想计算消息字段中重复“lupoK”的次数,即“message”:“第一个lupoK lupoK”使用MongoDB中的聚合,我使用的是studio3t接口 我的文件结构是— { "_id" : ObjectId("5df9c780b05196da93be262b"), "id" : "61a4c53a-aa99-4336-ab4f-07bb7f618889", "time" : "00:00:45", "
{
"_id" : ObjectId("5df9c780b05196da93be262b"),
"id" : "61a4c53a-aa99-4336-ab4f-07bb7f618889",
"time" : "00:00:45",
"username" : "siul",
"message" : "***first lupoK lupoK***",
"emoticon_place" : [
{
"_id" : "128428",
"begin" : NumberInt(6),
"end" : NumberInt(10)
}
],
"fragments" : [
{
"text" : "first "
},
{
"emoticon" : {
"emoticon_id" : "128428",
"emoticon_set_id" : ""
},
"text" : "***lupoK***"
},
{
"emoticon" : {
"emoticon_id" : "128428",
"emoticon_set_id" : ""
},
"text" : "***lupoK***"
}
]
}
提前感谢 这在mongo shell中起作用(假设消息
字段是字符串并且存在):
注: $split操作根据分隔符分割消息字符串-在本例中,分隔符为“lupoK”。分割返回一个由“lupoK”分隔的令牌数组。因此,令牌数减去1,给出了使用“lupoK”的次数,“lupoK”的出现次数 使用以下示例消息字符串检查结果:
"***first lupoK lupoK***"
"lupoKlupoK"
" lupoK lupoK "
""
"lupoKlupoKlupoK"
"lupoK"
"HELLO * lupoK* WORLD"
"HELLO WORLD"
"***first lupoK lupoKlupoK lupoK***lupoK *** last lupoK."
例如,某些字符串的标记:
"***first lupoK lupoK***"
"lupoKlupoK"
" lupoK lupoK "
""
"lupoKlupoKlupoK"
"lupoK"
"HELLO * lupoK* WORLD"
"HELLO WORLD"
"***first lupoK lupoKlupoK lupoK***lupoK *** last lupoK."
生成这三个令牌:“***first lupoK lupoK***”
[“***first”、“,”***“]
有以下两个标记:“HELLO*lupoK*WORLD”
[“HELLO*”,“*WORLD”]
有七个标记:“***first lupoK lupoK lupoK***lupoK***last lupoK.”
[“***first”、“、”、“、”、“***”和“***last”、“]