Java Spring MongoDB:基于日期聚合第一条记录
在我的应用程序中,每周对客户进行评估,我需要他们在特定分支代码和类别中的最新评估 在我的json数据中: 分支代码:Java Spring MongoDB:基于日期聚合第一条记录,java,mongodb,spring-data-mongodb,mongotemplate,Java,Mongodb,Spring Data Mongodb,Mongotemplate,在我的应用程序中,每周对客户进行评估,我需要他们在特定分支代码和类别中的最新评估 在我的json数据中: 分支代码:customerView.branchCode 类别:cardInfo.\u id 评估日期:date 对象(Json): 在这个示例中,我有两条记录具有相同的customerView.number和cardInfo.\u id,我需要具有5d2d57da43a99e1a5c4728d7 id的对象,因为该日期大于该日期 爪哇: 返回对象: 0 => id = 2
customerView.branchCode
类别:cardInfo.\u id
评估日期:date
对象(Json):
在这个示例中,我有两条记录具有相同的customerView.number
和cardInfo.\u id
,我需要具有5d2d57da43a99e1a5c4728d7 id的对象,因为该日期大于该日期
爪哇:
返回对象:
0 =>
id = 2374051 /*customerView.number*/
date = "2019-07-15T19:30:00.000Z"
totalScore = null
任何人都可以帮助我如何在客户编号中获取
totalScore
值?在组聚合中,as()
返回链GroupOption
对象,它可以基于第一个(…)
或最后一个(…)
记录完成其他字段
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
在组聚合中,
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
0 =>
id = 2374051 /*customerView.number*/
date = "2019-07-15T19:30:00.000Z"
totalScore = 11.0