Spring/error

Spring MongoDB - Cannot evaluate org.springframework.data.mongodb.core.aggregation.Aggregation.toString() 관련 에러

비뀨_ 2023. 4. 9. 01:10

버전 : Spring Data MongoDB 2.7.9

 

예외 메세지

Method threw 'java.lang.IllegalArgumentException' exception. 
Cannot evaluate org.springframework.data.mongodb.core.aggregation.Aggregation.toString()

해당 예외는 아래의 코드에서 발생했다

GroupOperation groupByRoomId = group(roomId).push(content).as(content);
LimitOperation limit = new LimitOperation(pageable.getPageSize());
SortOperation sort = Aggregation.sort(Sort.by(Sort.Order.desc(createdAt)));
MatchOperation matchStage = Aggregation.match(
                                Criteria.where(RoomColumn.PARTICIPATIONS.getWord())
                                .all(userId)
                            );
ProjectionOperation projection =
                Aggregation.project()
                .and(roomId).as(roomId)
                .and(content).as(content)
                .and(createdAt).as(createdAt);
Aggregation aggregation = Aggregation.newAggregation(
                projection, groupByRoomId, matchStage, sort, limit
        );
List<ChatRoomResDto> messages = mongoTemplate.aggregate(
                        aggregation,
                        "chat_room",
                        ChatRoomResDto.class)
                .getMappedResults();

 

간단한 예외였는데 

ProjectionOperation에서 적은 것과

mongoTemplate.aggregate 안에서 적은 Collection의 이름이 달라서 생긴 문제.

해당 Collection은 chat_room이 아니라 message 였어야 함!!

 

해당 컬렉션에 있는 필드와 Aggregation 에서 가져올 필드들이 맞는지 확인해보자!