【发布时间】:2017-08-30 10:34:25
【问题描述】:
我在使用 Spring Boot 从 MongoDB 检索数据时遇到以下问题。
这是我的架构:
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", "vegetable isvegetable or vegetables"}的输出应该是什么
-
@shubham 它应该算作 1。O/P 将是:{"vegetable" : 1 } 不是 {"vegetable" : 3}
-
在这种情况下 ==> {"Id7", "vegetable vegies Vegetarian"} ?
-
@shubham 在这种情况下:{"vegetable" : 1}, {"vegies" : 1}, {"vegetarian" : 1}
标签: regex mongodb spring-boot mongodb-query aggregation-framework