在我的应用中,每周对客户进行一次评估,我需要在特定分支代码及其类别中进行最新的评估。
在我的json数据中:
分支代码:customerView.branchCode
类别:cardInfo._id
评估日期:date
对象(杰森):
/* 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的对象,因为该日期大于该日期。
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
的价值?
答案 0 :(得分:0)
在组聚合中,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