首页 > 解决方案 > Spring MongoDB:基于日期聚合第一条记录

问题描述

在我的应用程序中,每周都会对客户进行评估,我需要他们在特定分支代码及其类别中的最新评估。

在我的 json 数据中:

分行代码:customerView.branchCode

类别 :cardInfo._id

评估日期:date

对象(Json):

/* 1 */
{
    "_id" : ObjectId("5d2c552443a99e1c2464b23a"),
    "customerView" : {
        "_id" : NumberLong(3278642838),
        "number" : NumberLong(2374051),
        "type" : 1,
        "branchCode" : "1100"
    },
    "cardInfo" : {
        "_id" : ObjectId("5cee1eaa9eae1e1330b5219c"),
        "modelId" : "5cecbbcd9eae1e0be0530ccb",

    },
    "totalScore" : 21.0,
    "date" : ISODate("2019-07-14T19:30:00.000Z"),
    "version" : 0,
    "_class" : "entity.CustomerAssessmentDocument"
}
,
/* 2 */
{
    "_id" : ObjectId("5d2d57da43a99e1a5c4728d7"),
    "customerView" : {
        "_id" : NumberLong(3278642838),
        "number" : NumberLong(2374051),
        "type" : 1,
        "branchCode" : "1100"
    },
    "cardInfo" : {
        "_id" : ObjectId("5cee1eaa9eae1e1330b5219c"),
        "modelId" : "5cecbbcd9eae1e0be0530ccb"

    },
    "totalScore" : 11.0,
    "date" : ISODate("2019-07-15T19:30:00.000Z"),
    "version" : 0,
    "_class" : "entity.CustomerAssessmentDocument"
}

在这个示例中,我有两条相同的记录,customerView.number并且cardInfo._id我需要具有 5d2d57da43a99e1a5c4728d7 id 的对象,因为那个日期大于那个日期。

爪哇:

public List<CustomerAssessmentDocument.CustomerAssessmentCustomerNumberProjection> getTopCustomersSortedByDateInBranch(
            String cardId, Set<String> branches) {

        final Criteria criteria =
                Criteria.where("totalScore").ne(0D)
                        .and("customerView.branchCode").in(branches)
                        .and("cardInfo._id").is(new ObjectId(cardId));

        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.sort(Sort.Direction.DESC, "date"),
                Aggregation.match(criteria),
                Aggregation.project("totalScore", "customerView", "date"),
                Aggregation.group("$customerView.number").first("date").as("date")
        );

        final AggregationResults<CustomerAssessmentDocument.CustomerAssessmentCustomerNumberProjection> customerAssessmentDocument =
                mongoTemplate.aggregate(
                        aggregation,
                        "CS_ASSESSMENT",
                        CustomerAssessmentDocument.CustomerAssessmentCustomerNumberProjection.class
                );
        return customerAssessmentDocument.getMappedResults();
    }

Java 类:

@Document(collection = "CS_ASSESSMENT")
public class CustomerAssessmentDocument extends AuditDocument {
    @Id
    private String id;
    private CustomerViewEnt customerView;
    private CardInfo cardInfo;
    private Double totalScore;
    private String description;
    private String assessmentReqId;
    private Date date;
    private Date effectiveDate;
...
    public class CustomerAssessmentCustomerNumberProjection {
        /**
         * CustomerAssessmentDocument->customerView.number
         */
        private Long id;
        /**
         * CustomerAssessmentDocument->date
         */
        private Date date;
        /**
         * CustomerAssessmentDocument->totalScore
         */
        private Double totalScore;
    }
}

返回对象:

0 =>
     id =  2374051 /*customerView.number*/
     date =  "2019-07-15T19:30:00.000Z"
     totalScore = null

任何人都可以帮助我如何totalScore在客户编号中获得价值?

标签: javamongodbspring-data-mongodbmongotemplate

解决方案


在组聚合中,as()返回链GroupOption对象,它可以完成其他字段的基础first(...)last(...)记录。

Aggregation.group("$customerView.number").first("totalScore").as("totalScore").first("date").as("date")

返回对象:

0 =>
    id =  2374051 /*customerView.number*/
    date =  "2019-07-15T19:30:00.000Z"
    totalScore = 11.0

推荐阅读