簡體   English   中英

分組然后排序

[英]grouping and then sorting

我准備將聚合進行分組,但不幸的是,我無法根據日期對輸出進行排序。 這是匯總

Aggregation aggregation = newAggregation(
            match(Criteria.where(CREATED_CRITERIA).gte(midnight).lte(now)),
            unwind(list),
            group(list + ".label").sum(list + ".value").as("value"),
             sort(Sort.Direction.ASC, "_id") 
            );

對象看起來像

{
    "_id" : ObjectId("56d9549e6a082cbc68dcedeb"),
    "created" : ISODate("2016-03-04T09:01:00.000Z"),
    "trucker" : NumberLong(0),
    "toProcess" : NumberLong(0),
    "smsSent" : NumberLong(0),
    "correct" : NumberLong(0),
    "redirected" : NumberLong(0),
    "truckerPerHours" : [ 
        {
            "label" : "Fri Mar 04 10:00:00 CET 2016",
            "value" : 0
        }
    ],
    "toProcessPerHours" : [ 
        {
            "label" : "Fri Mar 04 10:00:00 CET 2016",
            "value" : 0
        }
    ],
    "smsSentPerHours" : [ 
        {
            "label" : "Fri Mar 04 10:00:00 CET 2016",
            "value" : 0
        }
    ],
    "correctPerHours" : [ 
        {
            "label" : "Fri Mar 04 10:00:00 CET 2016",
            "value" : 0
        }
    ],
    "redirectedPerHours" : [ 
        {
            "label" : "Fri Mar 04 10:00:00 CET 2016",
            "value" : 0
        }
    ],
    "truckerPerBranch" : [],
    "toProcessPerBranch" : [],
    "smsSentPerBranch" : [],
    "correctPerBranch" : [],
    "redirectedPerBranch" : []
}

我想分組,然后根據“創建的”日期進行排序,此刻,我根據標簽進行排序,這不是一個好主意,因為它是一個字符串。

您可以使用$first運算符在$group管道階段中包括date字段,然后在該字段中對結果管道進行排序。 以下兩個示例說明了這種方法:


Mongo Shell:

pipeline = [
    { 
        "$match": {
            "created": { "$lte": now, "$gte": midnight }
        }
    },
    { "$unwind": "$smsSentPerHours" },
    {
        "$group": {
            "_id": "$smsSentPerHours.label",
            "value": { "$sum": "$smsSentPerHours.value" },
            "created": { "$first": "$created" }
        }
    },
    { "$sort": { "created": 1 } }
]

db.collection.aggregate(pipeline);

Spring Data MongoDB:

import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;

MongoTemplate mongoTemplate = repository.getMongoTemplate();
Aggregation agg = newAggregation(
    match(Criteria.where(CREATED_CRITERIA).gte(midnight).lte(now)),
    unwind(list),
    group(list + ".label")
        .sum(list + ".value").as("value")
        .first("created").as("created"),
    project("created").and("value").previousOperation(),
    sort(ASC, "created")
);

AggregationResults<OutputType> result = mongoTemplate.aggregate(agg,
                                            "collection", OutputType.class);
List<OutputType> mappedResult = result.getMappedResults();

您為什么不使用投影 ...請嘗試以下操作:

如果要返回具有聚合功能的實體列表,則可以使用Projections。 在標准中,這是通過ProjectionList和Projections完成的。 例如

    final ProjectionList props = Projections.projectionList();
    //your group by conditions here:
    props.add(Projections.groupProperty("group property 1"));
    props.add(Projections.groupProperty("group property 2"));
    //your criteria with group by conditions here:
    crit.setProjection(props);
    crit.add(Order.desc("created"));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM