2017-02-25 49 views
0

我有一個名爲「約會」的文件集合,我試圖將每天的約會數量組合在一起,並填充對象列表調用AppointmentSummary,在那裏將每天一個對象,我使用Spring引導來嘗試和實現這一點,但我不斷遇到問題。Spring引導mongoDB彙總piple行沒有找到類型的屬性

我已經在相同的包

AppointmentSummaryRepository.java

public interface AppointmentSummaryRepository extends 
MongoRepository<Appointment,String>, AppointmentSummaryRepositoryCustom { 

} 

AppointmentSummaryRepositoryCustom.java

public interface AppointmentSummaryRepositoryCustom { 

List<AppointmentSummary> aggregate(LocalDate startDate, LocalDate endDate); 

}

012創建了以下三個類

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

每當我運行它,我不斷收到以下錯誤:

org.springframework.data.mapping.PropertyReferenceException:沒有財產約定找到類型預約!

我相信問題發生在下面的代碼段中,我的理解是我初始化管道的不同階段並將它們傳遞給mongoTemplate,'getMappedResults'將映射來自兩個對象的字段並填充具有聚合管道輸出的AppointmentSummary.class?

return mongoTemplate.aggregate(Aggregation.newAggregation(
       matchOperation, 
       groupOperation, 
       projectionOperation 
     ), Appointment.class, AppointmentSummary.class).getMappedResults(); 

要注意對象約會沒有字段/屬性約會。我添加了這個,但是當我運行代碼時,我收到另一個錯誤消息,抱怨無法找到約會的類型日期。

在此先感謝您的幫助!

回答

1

使用以下變種蒙戈模板aggregate,其中collection name而不是class

mongoTemplate.aggregate(Aggregation.newAggregation(
      matchOperation, 
      groupOperation, 
      projectionOperation 
    ), "appointment", AppointmentSummary.class).getMappedResults(); 

的原因是,當你使用類型變種運行春季在聚合管道用於匹配的POJO的字段名的字段的驗證,當它沒有找到別名失敗。

+0

謝謝你解決了我的問題。 – teamerMan

相關問題