gpt4 book ai didi

java - Spring MongoDB : Aggregation first record base on date

转载 作者:太空宇宙 更新时间:2023-11-04 09:31:23 25 4
gpt4 key购买 nike

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

在我的 json 数据中:

分支代码:customerView.branchCode

类别:cardInfo._id

评估日期:日期

对象(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;
}
}

返回对象:

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

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

最佳答案

在组聚合中,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

关于java - Spring MongoDB : Aggregation first record base on date,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57052737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com