分组然后排序

时间:2016-03-04 10:27:11

标签: spring mongodb sorting grouping

我准备聚合到分组,但不幸的是我无法根据日期对输出进行排序。这是聚合

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" : []
}

我想基于“创建”日期进行分组然后排序,目前我根据标签进行排序,这不是一个好主意,因为它是一个字符串。

2 个答案:

答案 0 :(得分:1)

您可以使用 $group 运算符在 $first 管道阶段中包含日期字段,之后您可以对生成的管道进行排序在那个领域。显示这种方法的两个例子如下:

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

答案 1 :(得分:0)

为什么不使用预测 ...试试这个:

如果要返回具有agregation功能的实体列表,可以使用Projections。在标准中,这是通过ProjectionList和Projections完成的。 e.g。

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