The Sorts class provides static factory methods for the MongoDB sort criteria operators. Each method returns an instance of the Bson
type, which can in turn be passed to any method that expects sort criteria.
다음 코드와 같이 Sorts
클래스의 메서드를 정적으로 가져올 수 있습니다.
import org.mongodb.scala.model.Sorts._
이 가이드의 예제에서는 이러한 정적 가져오기를 가정합니다.
오름차순
오름차순 정렬을 지정하려면 ascending()
메서드 중 하나를 사용합니다.
다음 예에서는 quantity
필드에 오름차순 정렬을 지정합니다.
ascending("quantity")
다음 예시 에서는 quantity
필드 에 오름차순 정렬을 지정한 다음 totalAmount
필드 에 오름차순 정렬을 지정합니다.
ascending("quantity", "totalAmount")
내림차순
내림차순 정렬을 지정하려면 descending()
메서드 중 하나를 사용합니다.
다음 예에서는 quantity
필드에 내림차순 정렬을 지정합니다:
descending("quantity")
다음 예시 에서는 quantity
필드 에 내림차순 정렬을 지정한 다음 totalAmount
필드 에 descending
정렬을 지정합니다.
descending("quantity", "totalAmount")
텍스트 점수
$text
쿼리 의 점수 에 대한 정렬 을 지정 하려면 metaTextScore()
메서드 를 사용 하여 프로젝션된 필드 의 이름 을 지정 합니다 .
다음 예시 에서는 scoreValue
필드 에 프로젝션될 $text
쿼리 의 점수를 기준으로 내림차순 정렬을 지정합니다.
metaTextScore("scoreValue")
정렬 결합
여러 정렬 기준을 결합하려면 orderBy()
메서드를 사용합니다.
다음 예시 에서는 quantity
및 totalAmount
필드에 오름차순 정렬을 지정한 다음 orderDate
필드 에 내림차순 정렬을 지정합니다.
orderBy(ascending("quantity", "totalAmount"), descending("orderDate"))