【问题标题】:How to sum the array size across the entire collection?如何对整个集合的数组大小求和?
【发布时间】: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


    【解决方案1】:

    您可以将聚合简化为以下内容。

    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();
    

    【讨论】:

    • 您有机会验证答案吗?告诉我。
    【解决方案2】:

    这是一种方法

    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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-05
      • 2020-01-13
      • 1970-01-01
      • 2019-09-11
      • 2020-01-06
      • 1970-01-01
      • 2014-11-16
      • 1970-01-01
      相关资源
      最近更新 更多