如何在整个集合中求和数组大小?

问题描述 投票:1回答:2

我有一个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的新手,所以给我一个提示来解决这个问题。聚合是计算数组list.right的最佳方法吗?

spring mongodb aggregation-framework spring-data-mongodb
2个回答
0
投票

这是一种方法

Criteria criteria = Criteria.where("teamId").is("111");
MatchOperation matchOperation = match(criteria);
AggregationExpression aggregationExpression =  
                                    ArrayOperators.Size.lengthOfArray("utterances");
ProjectionOperation projectionOperation =   
                  project("scoreId", "teamId").and(aggregationExpression).as("sum");

GroupOperation groupOperation = group("teamId").sum("sum").as("totalSum");
Aggregation aggregation = Aggregation.newAggregation(matchOperation, projectionOperation, groupOperation);
AggregationResults<BasicDBObject> team = mongoTemplate.aggregate(aggregation, "team", BasicDBObject.class);

您可以使用pojo并映射字段,而不是BasicDBObject。


0
投票

您可以将汇总简化为以下内容。

db.teamScore.aggregate([
  {$match : {teamId:"111"}},
  {$group: {_id: null, "total_sum": {"$sum": {"$size":"$utterances"}}}}
])

春天代码:

Criteria creteria = Criteria.where("teamId").is("111");
MatchOperation matchStage = Aggregation.match(creteria);
GroupOperation group = group()
               .sum(ArrayOperators.Size.lengthOfArray("utterances")).as("count");
Aggregation aggregation = Aggregation.newAggregation(matchStage, group);
AggregationResults<Intent> result = mongoTemplate.aggregate(aggregation, "intents", Intent.class);
List<Intent> results = result.getMappedResults();
© www.soinside.com 2019 - 2024. All rights reserved.