문서 메뉴
문서 홈
/ / /
Java 동기화
/ /

프로젝션 빌더

이 페이지의 내용

  • 개요
  • 샘플 문서 및 예제
  • 프로젝션 작업
  • 포함
  • 제외
  • 프로젝션 결합
  • 제외 대상 _id
  • 배열 요소 일치 프로젝트
  • 배열 슬라이스 투영
  • 텍스트 점수 프로젝트

이 가이드에서는 MongoDB Java 드라이버에서빌더 를 사용하여 프로젝션 을 지정하는 방법을 배울 수 있습니다.

MongoDB는 필드 프로젝션을 지원하여 쿼리에서 결과를 반환할 때 포함할 필드와 제외할 필드를 지정할 수 있습니다. MongoDB의 프로젝션은 몇 가지 기본 규칙을 따릅니다:

  • 명시적으로 제외되지 않는 한,_id 필드는 항상 포함됩니다.

  • 포함할 필드를 지정하면 _id 필드를 제외한 다른 모든 필드가 암시적으로 제외됩니다.

  • 제외할 필드를 지정하면 쿼리 결과에서 해당 필드 제거됩니다.

프로젝션 메커니즘 에 대한 자세한 내용은여기를 참조하세요.

Projections 클래스는 모든 MongoDB 프로젝션 연산자에 대한 정적 팩토리 메서드를 제공합니다. 각 메서드는 프로젝션을 예상하는 모든 메서드에 전달할 수 있는 BSON 유형의 인스턴스를 반환합니다.

간결하게 하기 위해 프로젝션 클래스의 메서드를 정적으로(statically) 가져오도록 선택할 수 있습니다.

import static com.mongodb.client.model.Projections.*;

다음 예제에서는 이 정적 가져오기를 가정합니다.

다음 섹션에서는 projection_builders라는 샘플 컬렉션에 대해 쿼리 및 프로젝션 작업을 실행하는 예를 제공합니다. 각 섹션은 이름이 collection인 변수를 사용하여 projection_builders 컬렉션의 MongoCollection 인스턴스를 참조합니다.

이 컬렉션에는 2018년과 2019년의 월별 평균 기온을 섭씨 단위로 나타내는 다음 문서가 포함되어 있습니다.

{
"year" : 2018,
"type" : "even number but not a leap year",
"temperatures" : [
{ "month" : "January", "avg" : 9.765 },
{ "month" : "February", "avg" : 9.675 },
{ "month" : "March", "avg" : 10.004 },
{ "month" : "April", "avg" : 9.983 },
{ "month" : "May", "avg" : 9.747 },
{ "month" : "June", "avg" : 9.65 },
{ "month" : "July", "avg" : 9.786 },
{ "month" : "August", "avg" : 9.617 },
{ "month" : "September", "avg" : 9.51 },
{ "month" : "October", "avg" : 10.042 },
{ "month" : "November", "avg" : 9.452 },
{ "month" : "December", "avg" : 9.86 }
]
},
{
"year" : 2019,
"type" : "odd number, can't be a leap year",
"temperatures" : [
{ "month" : "January", "avg" : 10.023 },
{ "month" : "February", "avg" : 9.808 },
{ "month" : "March", "avg" : 10.43 },
{ "month" : "April", "avg" : 10.175 },
{ "month" : "May", "avg" : 9.648 },
{ "month" : "June", "avg" : 9.686 },
{ "month" : "July", "avg" : 9.794 },
{ "month" : "August", "avg" : 9.741 },
{ "month" : "September", "avg" : 9.84 },
{ "month" : "October", "avg" : 10.15 },
{ "month" : "November", "avg" : 9.84 },
{ "month" : "December", "avg" : 10.366 }
]
}

다음 섹션에는 사용 가능한 프로젝션 작업과 Projections 클래스를 사용하여 이를 구성하는 방법에 대한 정보가 포함되어 있습니다.

include() 메서드를 사용하여 필드를 하나 이상 포함하도록 지정합니다.

다음 예제에는 year 필드와 (암시적으로) _id 필드가 포함되어 있습니다.

Bson filter = Filters.empty();
Bson projection = include("year");
collection.find(filter).projection(projection).forEach(doc -> System.out.println(doc.toJson()));

다음 코드에서는 이 프로젝션의 출력을 보여 줍니다.

{"_id": {"$oid": "6042edc9f2b56342164e0790"}, "year": 2018}
{"_id": {"$oid": "6042edc9f2b56342164e0791"}, "year": 2019}

다음 예제에는 yeartype 필드와 (암시적으로) _id 필드가 포함되어 있습니다:

Bson filter = Filters.empty();
Bson projection = include("year", "type");
collection.find(filter).projection(projection).forEach(doc -> System.out.println(doc.toJson()));

다음 코드에서는 이 프로젝션의 출력을 보여 줍니다.

{"_id": {"$oid": "6042edc9f2b56342164e0790"}, "year": 2018, "type": "even number but not a leap year"}
{"_id": {"$oid": "6042edc9f2b56342164e0791"}, "year": 2019, "type": "odd number, can't be a leap year"}

하나 이상의 필드 제외를 지정하려면 exclude() 메서드를 사용합니다.

다음 예제에서는 temperatures 필드를 제외합니다:

Bson filter = Filters.empty();
Bson projection = exclude("temperatures");
collection.find(filter).projection(projection).forEach(doc -> System.out.println(doc.toJson()));

다음 코드에서는 이 프로젝션의 출력을 보여 줍니다.

{"_id": {"$oid": "6042edc9f2b56342164e0790"}, "year": 2018, "type": "even number but not a leap year"}
{"_id": {"$oid": "6042edc9f2b56342164e0791"}, "year": 2019, "type": "odd number, can't be a leap year"}

다음 예시에서는 typetemperatures 필드를 제외합니다.

Bson filter = Filters.empty();
Bson projection = exclude("temperatures", "type");
collection.find(filter).projection(projection).forEach(doc -> System.out.println(doc.toJson()));

다음 코드에서는 이 프로젝션의 출력을 보여 줍니다.

{"_id": {"$oid": "6042edc9f2b56342164e0790"}, "year": 2018}
{"_id": {"$oid": "6042edc9f2b56342164e0791"}, "year": 2019}

여러 프로젝션을 결합하려면 fields() 메서드를 사용합니다.

다음 예제에는 yeartype 필드가 포함되어 있으며 _id 필드는 제외됩니다:

Bson filter = Filters.empty();
Bson projection = fields(include("year", "type"), exclude("_id"));
collection.find(filter).projection(projection).forEach(doc -> System.out.println(doc.toJson()));

다음 코드에서는 이 프로젝션의 출력을 보여 줍니다.

{"year": 2018, "type": "even number but not a leap year"}
{"year": 2019, "type": "odd number, can't be a leap year"}

_id 필드 제외를 지정하려면 excludeId() 편의(convenience) 메서드를 사용하세요.

Bson filter = Filters.empty();
Bson projection = fields(include("year", "type"), excludeId());
collection.find(filter).projection(projection).forEach(doc -> System.out.println(doc.toJson()));

다음 코드에서는 이 프로젝션의 출력을 보여 줍니다.

{"year": 2018, "type": "even number but not a leap year"}
{"year": 2019, "type": "odd number, can't be a leap year"}

elemMatch(String, Bson) 메서드 변형을 사용하여 제공된 쿼리 필터와 일치하는 배열의 첫 번째 요소를 포함하는 배열 프로젝션을 지정합니다. 이 필터링은 쿼리 필터(제공된 경우)와 일치하는 모든 문서를 검색한 후에 발생합니다.

참고

일치 항목 수에 관계없이 지정된 쿼리 필터와 일치하는 첫 번째 요소만 포함됩니다.

다음 예제에서는 avg 필드가 10.1보다 큰 temperatures 배열의 첫 번째 요소를 프로젝션합니다.

Bson filter = Filters.empty();
Bson projection = fields(include("year"), elemMatch("temperatures", Filters.gt("avg", 10.1)));
collection.find(filter).projection(projection).forEach(doc -> System.out.println(doc.toJson()));

다음 코드에서는 이 프로젝션의 출력을 보여 줍니다.

{"_id": {"$oid": "6042edc9f2b56342164e0790"}, "year": 2018}
{"_id": {"$oid": "6042edc9f2b56342164e0791"}, "year": 2019, "temperatures": [{"month": "March", "avg": 10.43}]}

작업의 쿼리 부분에서 일치 조건을 지정한 경우 elemMatch(String) 메서드 변형을 사용하여 배열의 첫 번째 요소를 포함하도록 위치 투영을 지정합니다. 쿼리 필터와 일치하는 문서만 검색됩니다.

중요

MongoDB 4.4 미만 버전에서는 지정된 배열 필드가 쿼리 필터에 나타나야 합니다. MongoDB 4.4부터는 쿼리 필터에 나타나지 않는 배열 필드에 위치 프로젝트를 사용할 수 있습니다.

다음 예시에서는 temperatures 배열의 첫 번째 요소를 투영합니다.

Bson filter = Filters.gt("temperatures.avg", 10.1);
Bson projection = fields(include("year"), elemMatch("temperatures"));
collection.find(filter).projection(projection).forEach(doc -> System.out.println(doc.toJson()));

다음 코드에서는 이 프로젝션의 출력을 보여 줍니다.

{"_id": {"$oid": "6042edc9f2b56342164e0791"}, "year": 2019, "temperatures": [{"month": "March", "avg": 10.43}]}

slice() 메서드를 사용하여 배열의 일부를 프로젝션합니다.

다음 예시에서는 temperatures 배열의 처음 6개 요소를 투영합니다.

Bson filter = Filters.empty();
// first half of the year
Bson projection = slice("temperatures", 6);
collection.find(filter).projection(projection)
.forEach(doc -> System.out.println(doc.toJson(JsonWriterSettings.builder().indent(true).build())));

다음 코드에서는 이 프로젝션의 출력을 보여 줍니다.

{
"_id": {
"$oid": "6042f1bc8ee6fa2a84d2be69"
},
"year": 2018,
"type": "even number but not a leap year",
"temperatures": [
... <January-June temperature nested documents>
]
}
{
"_id": {
"$oid": "6042f1bc8ee6fa2a84d2be6a"
},
"year": 2019,
"type": "odd number, can't be a leap year",
"temperatures": [
... <January-June temperature nested documents>
]
}

다음 예제에서는 temperatures 배열의 처음 6개 요소를 건너뛰고 다음 6개 요소를 프로젝션합니다:

Bson filter = Filters.empty();
// second half of the year
Bson projection = slice("temperatures", 6, 6);
collection.find(filter).projection(projection)
.forEach(doc -> System.out.println(doc.toJson(JsonWriterSettings.builder().indent(true).build())));

다음 코드에서는 이 프로젝션의 출력을 보여 줍니다.

{
"_id": {
"$oid": "6042f1bc8ee6fa2a84d2be69"
},
"year": 2018,
"type": "even number but not a leap year",
"temperatures": [
... <July-December temperature nested documents>
]
}
{
"_id": {
"$oid": "6042f1bc8ee6fa2a84d2be6a"
},
"year": 2019,
"type": "odd number, can't be a leap year",
"temperatures": [
... <July-December temperature nested documents>
]
}

metaTextScore() 메서드를 사용 하여 텍스트 쿼리 점수의프로젝션을 지정합니다.

다음 예시에서는 텍스트 점수를 score 필드의 값으로 투영합니다.

Bson filter = Filters.text("even number");
Bson projection = fields(include("year"), metaTextScore("score"));
collection.find(filter).projection(projection).forEach(doc -> System.out.println(doc.toJson()));

다음 코드에서는 이 프로젝션의 출력을 보여 줍니다.

{"_id": {"$oid": "6042f1bc8ee6fa2a84d2be69"}, "year": 2018, "score": 1.25}
{"_id": {"$oid": "6042f1bc8ee6fa2a84d2be6a"}, "year": 2019, "score": 0.625}

돌아가기

인덱스 빌더