Bepoz
파즈의 공부 일기
Bepoz
전체 방문자
오늘
어제
  • 분류 전체보기 (232)
    • 공부 기록들 (85)
      • 우테코 (17)
    • Spring (85)
    • Java (43)
    • Infra (17)
    • 책 정리 (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
Bepoz

파즈의 공부 일기

Spring

MongoDB 특정 필드만 가져오게끔 하는 projection

2023. 8. 13. 23:30

MongoDB 특정 필드만 가져오게끔 하는 projection

개요

몽고 DB에서 document를 조회할 때 매치되는 doc의 모든 필드를 가져오지만 projection을 이용해 특정 필드만 가져올 수가 있다. 필드 수가 많은 collection에서 내가 원하는 데이터의 특정 필드값만 조회하고 싶은데, 전체 필드를 가져오는 것은 굉장히 비효율 적일 것이다. 그럴 때에 사용할 수 있다.

public class Person {
    @Id
    private String id;
    private Name name; //Name 클래스는 firstName과 lastName 이렇게 2개의 필드가 존재
    @Positive
    private int age;
    @CreatedDate
    private LocalDateTime createdAt;
    @LastModifiedDate
    private LocalDateTime modifiedAt;
}

사용하려는 Person collection의 정보는 위와 같다.


MongoDB Compass에서의 사용

image

위와 같이 2건의 데이터를 집어넣은 상태이다.
이 상황에서 age가 20인 데이터를 검색하면 딱 저 데이터들이 리턴될 것이다. 하지만 내가 원하는 값은 name만을 원한다면 project를 이용하면 된다.

아래는 mongoDB Compass에서 project를 사용했을 때의 결과이다.

image

원하는 필드에 1을 기입하면 출력이 된다. Name은 Object 타입이고 내부에 여러 필드가 있다.

image

이렇게 상세 필드를 기입하면 해당 필드만 뽑아올 수가 있다.

image

이번에는 아예 0으로 두었더니 해당 필드만 빼고 가져온 것을 확인할 수가 있었다.

1로 두었을 때에는 해당 필드만 가져오지만 0으로 두면 해당 필드를 제외하고 가져오게 된다.

image

1을 사용하게 되면 해당 필드말고도 _id 값을 가져왔었는데 이 또한 0으로 명시해서 가져오지 않을 수가 있다.


@Query에서의 사용

이번에는 @Query 어노테이션을 사용하여 projections을 사용해보겠다.

public interface PersonRepository extends MongoRepository<Person, String>, PersonRepositoryCustom {

    @Query(value = "{'age': 20}", fields = "{'name': 1, 'age': 1}")
    List<Person> findAllWithProjections();
}

---------------------------------------------------------------------------------

@GetMapping("/projections")
public List<Person> getPersonWithProjections() {
    return personRepository.findAllWithProjections();
}
[
    {
        "id": "64d8cb154360a130e3d5e422",
        "name": {
            "name": "negative Dont"
        },
        "age": 20,
        "createdAt": null,
        "modifiedAt": null
    },
    {
        "id": "64d8cb31f7736e129cf1fb9b",
        "name": {
            "name": "negative Dont"
        },
        "age": 20,
        "createdAt": null,
        "modifiedAt": null
    }
]

조회하면 필요한 값만 들고오는 것을 확인할 수 있다.
fields = "{'name': 1, '_id': 0}" 이런 식으로 변경했을 때에도 적절한 response를 돌려준다.


MongoTemplate과 Query에서의 사용

@PostMapping("/projections")
public List<Person> getPersonWithProjections(@RequestBody List<String> projections) {
    Query query = new Query();
    for (String projection : projections) {
        query.fields().include(projection);
    }
    return mongoTemplate.find(query, Person.class);
}

위와 같이 fields().include() 메서드를 이용할 수 있다. exclude() 를 통해 위에서 0을 설정한 결과값을 받을 수도 있다.

image

include의 설명을 보면 projection의 역할인 것을 확인할 수가 있다.


REFERENCE

https://www.mongodb.com/docs/manual/tutorial/project-fields-from-query-results/

'Spring' 카테고리의 다른 글

Spring Boot3에서의 @Enumerated(EnumType.STRING) 문제  (0) 2024.07.10
[JPA] JPA Auditing에서 OffsetDateTime 사용하기  (2) 2024.01.04
[Spring] Filter와 server.compression 설정을 통한 api 압축  (0) 2023.07.29
[Spring Batch] 이벤트 리스너 내용 정리  (0) 2023.05.04
[Spring Batch] Multi Thread Processing 내용 정리  (1) 2023.04.03
    'Spring' 카테고리의 다른 글
    • Spring Boot3에서의 @Enumerated(EnumType.STRING) 문제
    • [JPA] JPA Auditing에서 OffsetDateTime 사용하기
    • [Spring] Filter와 server.compression 설정을 통한 api 압축
    • [Spring Batch] 이벤트 리스너 내용 정리
    Bepoz
    Bepoz
    https://github.com/Be-poz/TIL

    티스토리툴바