개요
이 가이드에서는 MongoDB 코틀린(Kotlin) 드라이버의 빌더 를 사용하여 쿼리에 대한 정렬 기준 을 지정하는 방법에 대해 설명합니다.
정렬 기준은 MongoDB가 데이터를 정렬하는 데 사용하는 규칙입니다. 정렬 기준의 몇 가지 예는 다음과 같습니다:
가장 작은 숫자에서 가장 큰 숫자로
가장 이른 시간부터 가장 늦은 시간까지
이름별 알파벳 순서
빌더는 BSON 객체를 구성하는 데 도움이 되는 코틀린 (Kotlin) 운전자 에서 제공하는 클래스입니다. 학습 내용은 빌더 가이드 를 참조하세요.
빌더를 사용하여 쿼리에 대한 정렬 기준을 지정하는 방법을 알아보려면 이 가이드를 읽어보세요.
Kotlin 드라이버에서 정렬에 대한 기본 사항을 알아보려면 정렬 가이드를 참조하세요.
이 페이지의 예제에서는 다음 문서가 포함된 샘플 collection을 사용합니다.
{ "_id": 1, "date": "2022-01-03", "orderTotal": 17.86, "description": "1/2 lb cream cheese and 1 dozen bagels" }, { "_id": 2, "date": "2022-01-11", "orderTotal": 83.87, "description": "two medium vanilla birthday cakes" }, { "_id": 3, "date": "2022-01-11", "orderTotal": 19.49, "description": "1 dozen vanilla cupcakes" }, { "_id": 4, "date": "2022-01-15", "orderTotal": 43.62, "description": "2 chicken lunches and a diet coke" }, { "_id": 5, "date": "2022-01-23", "orderTotal": 60.31, "description": "one large vanilla and chocolate cake" }, { "_id": 6, "date": "2022-01-23", "orderTotal": 10.99, "description": "1 bagel, 1 orange juice, 1 muffin" }
이 데이터는 다음 Kotlin 데이터 클래스로 모델링됩니다.
data class Order( val id: Int, val date: String, val orderTotal: Double, val description: String, )
팁
빌더 메서드 및 데이터 클래스 속성
애플리케이션 에 선택적 코틀린 (Kotlin) 운전자 확장 종속성을 추가하여 빌더 클래스의 메서드를 데이터 클래스 속성과 함께 직접 사용할 수 있습니다. 자세한 내용을 학습 예제를 보려면 데이터 클래스와 함께 빌더 사용 가이드 참조하세요.
Sorts 클래스
Sorts 클래스는 MongoDB에서 지원하는 모든 정렬 기준 연산자에 대한 정적 팩토리 메서드를 제공하는 빌더입니다. 이러한 메서드는 FindFlow 인스턴스의 sort() 메서드 또는 Aggregates.sort() 에 전달할 수 있는 Bson 객체를 반환합니다.
Aggregates 클래스에 대해 자세히 알아보려면 애그리게이션 빌더 가이드를 참조하세요.
이 섹션의 클래스 및 인터페이스에 대한 자세한 내용은 다음 API 설명서를 참조하세요.
오름차순
오름차순 정렬을 지정하려면 Sorts.ascending() 정적 팩토리 메서드를 사용합니다. 정렬하려는 필드의 이름을 Sorts.ascending() 에 전달합니다.
다음 예에서는 샘플 컬렉션 의 문서를 orderTotal 필드에서 오름차순으로 정렬합니다.
val resultsFlow = collection.find() .sort(Sorts.ascending(Order::orderTotal.name)) resultsFlow.collect { println(it) }
Order(id=6, date=2022-01-23, orderTotal=10.99, description=1 bagel, 1 orange juice, 1 muffin) Order(id=1, date=2022-01-03, orderTotal=17.86, description=1/2 lb cream cheese and 1 dozen bagels) Order(id=3, date=2022-01-11, orderTotal=19.49, description=1 dozen vanilla cupcakes) Order(id=4, date=2022-01-15, orderTotal=43.62, description=2 chicken lunches and a diet coke) Order(id=5, date=2022-01-23, orderTotal=60.31, description=one large vanilla and chocolate cake) Order(id=2, date=2022-01-11, orderTotal=83.87, description=two medium vanilla birthday cakes)
내림차순
내림차순 정렬을 지정하려면 Sorts.descending() 정적 팩토리 메서드를 사용합니다. 정렬하려는 필드의 이름을 Sorts.descending() 에 전달합니다.
다음 예에서는 샘플 컬렉션 의 문서를 orderTotal 필드에서 내림차순으로 정렬합니다.
val resultsFlow = collection.find() .sort(Sorts.descending(Order::orderTotal.name)) resultsFlow.collect { println(it) }
Order(id=2, date=2022-01-11, orderTotal=83.87, description=two medium vanilla birthday cakes) Order(id=5, date=2022-01-23, orderTotal=60.31, description=one large vanilla and chocolate cake) Order(id=4, date=2022-01-15, orderTotal=43.62, description=2 chicken lunches and a diet coke) Order(id=3, date=2022-01-11, orderTotal=19.49, description=1 dozen vanilla cupcakes) Order(id=1, date=2022-01-03, orderTotal=17.86, description=1/2 lb cream cheese and 1 dozen bagels) Order(id=6, date=2022-01-23, orderTotal=10.99, description=1 bagel, 1 orange juice, 1 muffin)
정렬 기준 결합
정렬 기준을 결합하려면 Sorts.orderBy() 정적 팩토리 메서드를 사용하세요. 이 메서드는 정렬된 정렬 기준 목록을 포함하는 객체를 생성합니다. 정렬을 수행할 때 이전 정렬 기준이 동점인 경우 목록의 다음 정렬 기준을 사용하여 순서를 결정합니다.
다음 예시에서는 샘플 collection 의 문서를 date 필드에서는 내림차순으로 정렬하고, 동점인 경우 orderTotal 필드에서는 오름차순으로 정렬합니다.
val orderBySort = Sorts.orderBy( Sorts.descending(Order::date.name), Sorts.ascending(Order::orderTotal.name) ) val results = collection.find().sort(orderBySort) results.collect {println(it) }
Order(id=6, date=2022-01-23, orderTotal=10.99, description=1 bagel, 1 orange juice, 1 muffin) Order(id=5, date=2022-01-23, orderTotal=60.31, description=one large vanilla and chocolate cake) Order(id=4, date=2022-01-15, orderTotal=43.62, description=2 chicken lunches and a diet coke) Order(id=3, date=2022-01-11, orderTotal=19.49, description=1 dozen vanilla cupcakes) Order(id=2, date=2022-01-11, orderTotal=83.87, description=two medium vanilla birthday cakes) Order(id=1, date=2022-01-03, orderTotal=17.86, description=1/2 lb cream cheese and 1 dozen bagels)
텍스트 점수
검색 결과가 검색 문자열과 얼마나 일치하는지를 나타내는 텍스트 점수를 기준으로 텍스트 검색 결과를 정렬할 수 있습니다. 텍스트 검색의 텍스트 점수를 기준으로 정렬을 지정하려면 Sorts.metaTextScore() 정적 팩토리 메서드를 사용하세요.
Sorts.metaTextScore() 메서드를 사용하여 정렬 기준을 지정하는 방법을 보여주는 자세한 예는 정렬 가이드 의 텍스트 검색 섹션 을 참조하세요.
자세한 내용은 Sorts 클래스 API 설명서를 참조하세요.