tea*_*Man 1 java spring mongodb aggregation-framework
我有一个称为“约会”的文档集合,我正在尝试将每天的约会数量分组在一起,并填充一个称为AppointmentSummary的对象列表,其中每天将有一个对象,我正在使用Spring boot尝试并实现这一点,但是我一直遇到问题。
我在同一包中创建了以下三个类
AppointmentSummaryRepository.java
public interface AppointmentSummaryRepository extends
MongoRepository<Appointment,String>, AppointmentSummaryRepositoryCustom {
}
Run Code Online (Sandbox Code Playgroud)
AppointmentSummaryRepositoryCustom.java
public interface AppointmentSummaryRepositoryCustom {
List<AppointmentSummary> aggregate(LocalDate startDate, LocalDate endDate);
Run Code Online (Sandbox Code Playgroud)
}
AppointmentSummaryRepositoryImpl.java
public class AppointmentSummaryRepositoryImpl implements AppointmentSummaryRepositoryCustom {
private final MongoTemplate mongoTemplate;
private final Logger log = LoggerFactory.getLogger(AppointmentSummaryRepositoryImpl.class);
@Autowired
public AppointmentSummaryRepositoryImpl(MongoTemplate mongoTemplate){
this.mongoTemplate = mongoTemplate;
}
@Override
public List<AppointmentSummary> aggregate(LocalDate startDate, LocalDate endDate){
log.debug("This is a request to aggerate appointment summary between {} to {}", startDate.toString(), endDate.toString());
MatchOperation matchOperation = getMatchOperation(startDate, endDate);
GroupOperation groupOperation = getGroupOperation();
log.debug("End group operaton");
ProjectionOperation projectionOperation = getProjectOperation();
return mongoTemplate.aggregate(Aggregation.newAggregation(
matchOperation,
groupOperation,
projectionOperation
), Appointment.class, AppointmentSummary.class).getMappedResults();
}
private MatchOperation getMatchOperation(LocalDate startDate, LocalDate endDate) {
log.debug("Begin Match Operation");
Criteria appointmentCriteria = where("appointment_date").gt(startDate).andOperator(where("appointment_date").lt(endDate));
log.debug("End Match Operation");
return match(appointmentCriteria);
}
private GroupOperation getGroupOperation() {
log.debug("Performing Group Operation");
return group("appointment_date")
.last("appointment_date").as("appointment_date")
.addToSet("id").as("appointmentIds")
.sum("id").as("count");
}
private ProjectionOperation getProjectOperation() {
log.debug("Begin project operation");
return project("appointment_date","appointmentIds","count")
.and("appointment_date").previousOperation();
}
Run Code Online (Sandbox Code Playgroud)
每当我运行它时,都会不断出现以下错误:
org.springframework.data.mapping.PropertyReferenceException:找不到类型约会的属性约会!
我相信问题发生在以下代码段中,我的理解是,我初始化了管道的不同阶段,并将它们传递给mongoTemplate,“ getMappedResults”将映射两个对象中的字段并填充AppointmentSummary.class与聚合管道的输出?
return mongoTemplate.aggregate(Aggregation.newAggregation(
matchOperation,
groupOperation,
projectionOperation
), Appointment.class, AppointmentSummary.class).getMappedResults();
Run Code Online (Sandbox Code Playgroud)
注意,对象约会没有字段/属性约会。我在其中添加了代码,但是在运行代码时,我收到另一条错误消息,抱怨找不到约会的类型日期。
在此先感谢您的帮助!
下面使用模板蒙哥变种aggregate这需要collection name代替class。
mongoTemplate.aggregate(Aggregation.newAggregation(
matchOperation,
groupOperation,
projectionOperation
), "appointment", AppointmentSummary.class).getMappedResults();
Run Code Online (Sandbox Code Playgroud)
原因是,当您使用类型化的变体时,spring会对聚合管道中使用的字段运行验证以匹配pojo中的字段名称,并且在找不到别名时失败。
| 归档时间: |
|
| 查看次数: |
828 次 |
| 最近记录: |