Spring MongoDB : 聚合第一个记录基于日期

Spring MongoDB : Aggregation first record base on date

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

在我的 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.numbercardInfo._id 的记录,我需要具有 5d2d57da43a99e1a5c4728d7 id 的对象,因为那个日期大于那个日期。

Java:

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;
    }
}

return 对象:

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

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

组聚合中,as() return链GroupOption对象,可以根据first(...)last(...)记录完成其他字段

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

return 对象:

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