Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs Menu
Docs Home
/ / /
Kotlin 코루틴
/ /

프로젝션 빌더

이 가이드 에서는 MongoDB 코틀린 (Kotlin) 운전자 에서빌더 를 사용하여 프로젝션 을 지정하는 방법을 학습 수 있습니다.

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

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

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

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

프로젝션 메커니즘에 대한 자세한 내용은 MongoDB Server 문서의 쿼리에서 반환할 프로젝트 필드 가이드 를 참조하세요.

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

For brevity, you may choose to import the methods of the Projections class:

import 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 }
]
}

다음 데이터 클래스는 collection의 문서를 나타내는 데 사용됩니다.

data class YearlyTemperature(
@BsonId val id: ObjectId,
val year: Int,
val type: String,
val temperatures: List<MonthlyTemperature>
) {
data class MonthlyTemperature(
val month: String,
val avg: Double
)
}

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

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

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

data class Results(@BsonId val id: ObjectId, val year: Int)
val filter = Filters.empty()
val projection = Projections.include(YearlyTemperature::year.name)
val resultsFlow = collection.find<Results>(filter).projection(projection)
resultsFlow.collect { println(it) }
Results(id=6467808db5003e6354a1ee22, year=2018)
Results(id=6467808db5003e6354a1ee23, year=2019)

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

data class Results(@BsonId val id: ObjectId, val year: Int, val type: String)
val filter = Filters.empty()
val projection = Projections.include(YearlyTemperature::year.name, YearlyTemperature::type.name)
val resultsFlow = collection.find<Results>(filter).projection(projection)
resultsFlow.collect { println(it) }
Results(id=646780e3311323724f69a907, year=2018, type=even number but not a leap year)
Results(id=646780e3311323724f69a908, year=2019, type=odd number, can't be a leap year)

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

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

data class Results(@BsonId val id: ObjectId, val year: Int, val type: String)
val filter = Filters.empty()
val projection = Projections.exclude(YearlyTemperature::temperatures.name)
val resultsFlow = collection.find<Results>(filter).projection(projection)
resultsFlow.collect { println(it) }
Results(id=6462976102c85b29a7bfc9d5, year=2018, type=even number but not a leap year)
Results(id=6462976102c85b29a7bfc9d6, year=2019, type=odd number, can't be a leap year)

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

data class Results(@BsonId val id: ObjectId, val year: Int)
val filter = Filters.empty()
val projection = Projections.exclude(YearlyTemperature::temperatures.name, YearlyTemperature::type.name)
val resultsFlow = collection.find<Results>(filter).projection(projection)
resultsFlow.collect { println(it) }
Results(id=64629783d7760d2365215147, year=2018)
Results(id=64629783d7760d2365215148, year=2019)

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

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

data class Results(val year: Int, val type: String)
val filter = Filters.empty()
val projection = Projections.fields(
Projections.include(YearlyTemperature::year.name, YearlyTemperature::type.name),
Projections.excludeId()
)
val resultsFlow = collection.find<Results>(filter).projection(projection)
resultsFlow.collect { println(it) }
Results(year=2018, type=even number but not a leap year)
Results(year=2019, type=odd number, can't be a leap year)

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

data class Results(val year: Int, val type: String, val temperatures: List<YearlyTemperature.MonthlyTemperature>)
val filter = Filters.empty()
val projection = Projections.excludeId()
val resultsFlow = collection.find<Results>(filter).projection(projection)
resultsFlow.collect { println(it) }
Results(year=2018, type=even number but not a leap year, temperatures=[MonthlyTemperature(month=January, avg=9.765), MonthlyTemperature(month=February, avg=9.675), MonthlyTemperature(month=March, avg=10.004), MonthlyTemperature(month=April, avg=9.983), MonthlyTemperature(month=May, avg=9.747), MonthlyTemperature(month=June, avg=9.65), MonthlyTemperature(month=July, avg=9.786), MonthlyTemperature(month=August, avg=9.617), MonthlyTemperature(month=September, avg=9.51), MonthlyTemperature(month=October, avg=10.042), MonthlyTemperature(month=November, avg=9.452), MonthlyTemperature(month=December, avg=9.86)])
Results(year=2019, type=odd number, can't be a leap year, temperatures=[MonthlyTemperature(month=January, avg=10.023), MonthlyTemperature(month=February, avg=9.808), MonthlyTemperature(month=March, avg=10.43), MonthlyTemperature(month=April, avg=10.175), MonthlyTemperature(month=May, avg=9.648), MonthlyTemperature(month=June, avg=9.686), MonthlyTemperature(month=July, avg=9.794), MonthlyTemperature(month=August, avg=9.741), MonthlyTemperature(month=September, avg=9.84), MonthlyTemperature(month=October, avg=10.15), MonthlyTemperature(month=November, avg=9.84), MonthlyTemperature(month=December, avg=10.366)])

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

참고

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

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

data class Results(
val year: Int,
val temperatures: List<YearlyTemperature.MonthlyTemperature>?
)
val filter = Filters.empty()
val projection = Projections.fields(
Projections.include(YearlyTemperature::year.name),
Projections.elemMatch(
YearlyTemperature::temperatures.name,
Filters.gt(YearlyTemperature.MonthlyTemperature::avg.name, 10.1)
)
)
val resultsFlow = collection.find<Results>(filter).projection(projection)
resultsFlow.collect { println(it) }
Results(year=2018, temperatures=null)
Results(year=2019, temperatures=[MonthlyTemperature(month=March, avg=10.43)])

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

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

data class Results(
val year: Int,
val temperatures: List<YearlyTemperature.MonthlyTemperature>
)
val filter = Filters.gt(
"${YearlyTemperature::temperatures.name}.${YearlyTemperature.MonthlyTemperature::avg.name}",
10.1
)
val projection = Projections.fields(
Projections.include(YearlyTemperature::year.name),
Projections.elemMatch(YearlyTemperature::temperatures.name)
)
val resultsFlow = collection.find<Results>(filter).projection(projection)
resultsFlow.collect { println(it) }
Results(year=2019, temperatures=[MonthlyTemperature(month=March, avg=10.43)])

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

다음 예시에서는 temperatures 배열의 처음 6개 요소를 프로젝트합니다.

data class Results(val temperatures: List<YearlyTemperature.MonthlyTemperature>)
val filter = Filters.empty()
// First half of the year
val projection = Projections.fields(
Projections.slice(YearlyTemperature::temperatures.name, 6),
Projections.excludeId()
)
val resultsFlow = collection.find<Results>(filter)
.projection(projection)
resultsFlow.collect { println(it) }
Results(temperatures=[MonthlyTemperature(month=January, avg=9.765), MonthlyTemperature(month=February, avg=9.675), MonthlyTemperature(month=March, avg=10.004), MonthlyTemperature(month=April, avg=9.983), MonthlyTemperature(month=May, avg=9.747), MonthlyTemperature(month=June, avg=9.65)])
Results(temperatures=[MonthlyTemperature(month=January, avg=10.023), MonthlyTemperature(month=February, avg=9.808), MonthlyTemperature(month=March, avg=10.43), MonthlyTemperature(month=April, avg=10.175), MonthlyTemperature(month=May, avg=9.648), MonthlyTemperature(month=June, avg=9.686)])

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

data class Results(val temperatures: List<YearlyTemperature.MonthlyTemperature>)
val filter = Filters.empty()
// Second half of the year
val projection = Projections.fields(
Projections.slice(YearlyTemperature::temperatures.name, 6, 6),
Projections.excludeId()
)
val resultsFlow = collection.find<Results>(filter)
.projection(projection)
resultsFlow.collect { println(it) }
Results(temperatures=[MonthlyTemperature(month=July, avg=9.786), MonthlyTemperature(month=August, avg=9.617), MonthlyTemperature(month=September, avg=9.51), MonthlyTemperature(month=October, avg=10.042), MonthlyTemperature(month=November, avg=9.452), MonthlyTemperature(month=December, avg=9.86)])
Results(temperatures=[MonthlyTemperature(month=July, avg=9.794), MonthlyTemperature(month=August, avg=9.741), MonthlyTemperature(month=September, avg=9.84), MonthlyTemperature(month=October, avg=10.15), MonthlyTemperature(month=November, avg=9.84), MonthlyTemperature(month=December, avg=10.366)])

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

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

data class Results(val year: Int, val score: Double)
val filter = Filters.text("even number")
val projection = Projections.fields(
Projections.include(YearlyTemperature::year.name),
Projections.metaTextScore("score")
)
val resultsFlow = collection.find<Results>(filter).projection(projection)
resultsFlow.collect { println(it) }
Results(year=2018, score=1.25)
Results(year=2019, score=0.625)

돌아가기

Indexes

이 페이지의 내용