Regex 如何在mongodb中获得与给定单词匹配的所有子字符串及其计数?

Regex 如何在mongodb中获得与给定单词匹配的所有子字符串及其计数?,regex,mongodb,spring-boot,mongodb-query,aggregation-framework,Regex,Mongodb,Spring Boot,Mongodb Query,Aggregation Framework,我在使用spring boot从MongoDB检索数据时遇到以下问题 db.Item.aggregate([ { $match:{ description:/veg/gi } }, { $project :{ matchedAndUniqWords:{ $reduce:{ input:{ $filter:{input:{$split:[{"$toLower":"$description"}," "]

我在使用spring boot从MongoDB检索数据时遇到以下问题

db.Item.aggregate([ 
   { $match:{ description:/veg/gi } },
   { 
     $project :{
        matchedAndUniqWords:{
             $reduce:{
               input:{ $filter:{input:{$split:[{"$toLower":"$description"}," "]},as:"w",cond:{$ne:[{$indexOfCP:["$$w","veg"]},-1]}}},
               initialValue:[],
               in:{
                   $cond:[{$in:["$$this","$$value"]},{$concatArrays:[[],"$$value"]},{$concatArrays:[["$$this"],"$$value"]}]
                  }     
           }
        }
       }
     },
     {
      $unwind:{path : "$matchedAndUniqWords"}
   },
   {
     $group:{_id:"$matchedAndUniqWords",count:{"$sum":1}}
   }]);
这是我的模式:

class Item
{
    @Id
    String _id;
    String description;
}
假设数据库包含以下内容:

{"Id1", "carrot vegetable"},
{"Id2", "vegies is a brand"},
{"Id3", "I am Vegetarian"},
{"Id4", "Potato vegetable"},
{"Id5", "Fruits"}
我想得到的是以“veg”开头的术语和它们的数量。 这是这样的:

{"vegetable", 2},
{"vegies", 1},
{"vegetarian", 1}
到目前为止,我遇到了IndexOfCP操作,它可以从字符串中找到子字符串

db.Item.aggregate([ { $match:{ description:/veg/gi } }, { $project:{ index:{ $indexOfCP:[ { $toLower:"$description" }, "veg" ] }, description:1 } }, { $sort:{ index:1 } } ])
但是我在结果集中找不到匹配项及其计数


如何在mongo命令和spring boot中实现这一点。

在{“Id6”,“蔬菜是蔬菜或蔬菜”}@shubham的情况下,输出应该是什么?它应该计为1。O/P将是:{“蔬菜”:1}而不是{“蔬菜”:3},在这种情况下==>{“Id7”,“蔬菜-蔬菜-素食者”}@shubham在这种情况下:{“蔬菜”:1},{“蔬菜”:1},{“素食者”:1}在{“Id6”,“蔬菜是蔬菜或蔬菜”}@shubham的情况下,输出应该是多少?@shubham它应该算作1。O/P将是:{“蔬菜”:1}而不是{“蔬菜”:3},在这种情况下==>{“Id7”,“蔬菜蔬菜素食者”}?@shubham在这种情况下:{“蔬菜”:1},{“蔬菜”:1},{“素食者”:1}请您解释一下这个查询。这个查询拆分描述,然后只过滤与您的查询匹配的词,然后它减少这个结果数组以生成唯一的匹配词,正如您所说的,这个查询的输出应该是{“Id6”,“蔬菜是蔬菜或蔬菜”}应该是{“蔬菜”:1}然后在展开阶段,它展开匹配且唯一的关键字数组,然后对相似的单词进行分组。这将仅适用于mongodb 3.4或更高版本3.4。非常感谢@shubham为您提供答案。这正是我所希望的,尽管理解这一点需要一些时间。请您解释一下这个查询。这个查询会分割描述,然后用您的查询只过滤匹配的单词,然后它会减少这个结果数组以生成唯一和匹配的单词,正如您所说的,它的输出应该是{“Id6”,“蔬菜是蔬菜或蔬菜”}作为{“蔬菜”:1}然后在展开阶段,它展开匹配且唯一的关键字数组,然后它组相似的单词。这将仅适用于mongodb 3.4或大于3.4。非常感谢@shubham的回答。这正是我希望的,尽管需要一些时间才能理解这一点。
db.Item.aggregate([ 
   { $match:{ description:/veg/gi } },
   { 
     $project :{
        matchedAndUniqWords:{
             $reduce:{
               input:{ $filter:{input:{$split:[{"$toLower":"$description"}," "]},as:"w",cond:{$ne:[{$indexOfCP:["$$w","veg"]},-1]}}},
               initialValue:[],
               in:{
                   $cond:[{$in:["$$this","$$value"]},{$concatArrays:[[],"$$value"]},{$concatArrays:[["$$this"],"$$value"]}]
                  }     
           }
        }
       }
     },
     {
      $unwind:{path : "$matchedAndUniqWords"}
   },
   {
     $group:{_id:"$matchedAndUniqWords",count:{"$sum":1}}
   }]);