【发布时间】:2018-02-28 05:23:14
【问题描述】:
我有 mongodb json 数据:
{
"_id" : ObjectId("1111111111111111"),
"teamId" : "111",
"scoreId" : "50dcefb52d764ca3913985a80a4162ef",
"utterances" : [
{
"text" : "test1",
"parsedText" : "test1"
},
{
"text" : "test2",
"parsedText" : "test2"
}
]
},
{
"_id" : ObjectId("22222222222222"),
"teamId" : "111",
"scoreId" : "60dcefb52d764ca3913985a80a4162ef",
"utterances" : [
{
"text" : "do it1",
"parsedText" : "do it1"
},
{
"text" : "do it2",
"parsedText" : "do it2"
},
{
"text" : "do it3",
"parsedText" : "do it3"
}
]
}
而mongo查询是这样的:
db.teamScore.aggregate([
{$match : {teamId:"111"}},
{$unwind: '$utterances'},
{$group: {_id: '$_id', 'sum': { $sum: 1}}},
{$group: {_id: null, total_sum: {'$sum': '$sum'}}}
])
===>5
我想做的是使用spring mongo聚合的总和
我正在尝试制作如下来源:
Criteria creteria = Criteria.where("teamId").is("111");
MatchOperation matchStage = Aggregation.match(creteria);
GroupOperation group = group("scoreId")
.push("$utterances").as("utterances");
ProjectionOperation projectStage = project().and("scoreId").arrayElementAt(0).as("scoreId")
.and("utterances").size().as("count");
Aggregation aggregation = Aggregation.newAggregation(matchStage, unwind("utterances", true), group, projectStage);
AggregationResults<Intent> result = mongoTemplate.aggregate(aggregation, "intents", Intent.class);
List<Intent> results = result.getMappedResults();
我如何计算话语? 我是 mongodb 的新手,所以给我一个提示来解决这个问题。 聚合是计算数组列表的最佳方法,对吧?
【问题讨论】:
标签: spring mongodb aggregation-framework spring-data-mongodb