개요
이 가이드 에서는 Java Reactive Streams 운전자 의 빌더 사용하여 쿼리에 대한 정렬 기준을 지정하는 방법을 학습 수 있습니다.
정렬 기준은 MongoDB가 데이터를 정렬하는 데 사용하는 규칙입니다. 정렬 기준의 몇 가지 예는 다음과 같습니다:
가장 작은 숫자에서 가장 큰 숫자로
가장 이른 시간부터 가장 늦은 시간까지
이름별 알파벳 순서
빌더는 BSON 객체를 구성하는 데 도움이 되는 Java Reactive Streams 운전자 에서 제공하는 클래스입니다. 자세한 학습 은 빌더 가이드 참조하세요.
팁
간결하게 하기 위해 Sorts 클래스의 모든 메서드를 정적으로 가져오도록 선택할 수 있습니다.
import static com.mongodb.client.model.Sorts.*;
다음 예시에서는 이 정적 가져오기를 가정합니다.
이 페이지의 예제에서는 다음 문서가 포함된 샘플 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 strawberry birthday cakes" }, { "_id": 3, "date": "2022-01-11", "orderTotal": 19.49, "description": "1 dozen strawberry 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": 10.99, "description": "1 bagel, 1 orange juice, 1 muffin" }, { "_id": 6, "date": "2022-01-23", "orderTotal": 60.31, "description": "one large strawberry and chocolate cake" }
Sorts 클래스
Sorts 클래스는 MongoDB 에서 지원하는 모든 정렬 기준 연산자에 대한 정적 팩토리 메서드를 제공하는 빌더입니다. 이러한 메서드는 인스턴스 의 메서드 Bson sort() FindPublisher 또는 에 전달할 수 있는 객체 Aggregates.sort() 반환합니다.Aggregates 클래스에 대해 자세히 학습 보려면 애그리게이션 빌더 가이드 참조하세요.
이 섹션의 클래스 및 인터페이스에 대한 자세한 내용은 다음 API 설명서를 참조하세요.
오름차순
오름차순 정렬을 지정하려면 Sorts.ascending() 정적 팩토리 메서드를 사용합니다. 정렬할 필드의 이름을 Sorts.ascending() 에 전달합니다.
다음 예에서는 샘플 컬렉션 의 문서를 _id 필드에서 오름차순으로 정렬합니다.
FindPublisher<Document> findPublisher = collection.find().sort(ascending("_id")); Flux.from(findPublisher) .doOnNext(doc -> System.out.println(doc.toJson())) .blockLast();
{ "_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 strawberry birthday cakes" } { "_id": 3, "date": "2022-01-11", "orderTotal": 19.49, "description": "1 dozen strawberry cupcakes" } ...
내림차순
내림차순 정렬을 지정하려면 Sorts.descending() 정적 팩토리 메서드를 사용합니다. 정렬할 필드의 이름을 Sorts.descending() 에 전달합니다.
다음 예에서는 샘플 컬렉션 의 문서를 _id 필드에서 내림차순으로 정렬합니다.
FindPublisher<Document> findPublisher = collection.find().sort(descending("_id")); Flux.from(findPublisher) .doOnNext(doc -> System.out.println(doc.toJson())) .blockLast();
{ "_id": 6, "date": "2022-01-23", "orderTotal": 60.31, "description": "one large strawberry and chocolate cake" } { "_id": 5, "date": "2022-01-23", "orderTotal": 10.99, "description": "1 bagel, 1 orange juice, 1 muffin" } { "_id": 4, "date": "2022-01-15", "orderTotal": 43.62, "description": "2 chicken lunches and a diet coke" } ...
정렬 기준 결합
정렬 기준을 결합하려면 Sorts.orderBy() 정적 팩토리 메서드를 사용합니다. 이 메서드는 정렬된 정렬 기준 목록을 포함하는 객체를 구성합니다. 정렬을 수행할 때 가장 왼쪽 정렬 기준이 동점인 경우 목록의 다음 정렬 기준을 사용하여 순서를 결정합니다.
다음 예시에서는 샘플 collection 의 문서를 date 필드에서는 내림차순으로 정렬하고, 동점인 경우 orderTotal 필드에서는 오름차순으로 정렬합니다.
Bson orderBySort = orderBy(descending("date"), ascending("orderTotal")); FindPublisher<Document> findPublisher = collection.find().sort(orderBySort); Flux.from(findPublisher) .doOnNext(doc -> System.out.println(doc.toJson())) .blockLast();
{ "_id": 5, "date": "2022-01-23", "orderTotal": 10.99, "description": "1 bagel, 1 orange juice, 1 muffin" } { "_id": 6, "date": "2022-01-23", "orderTotal": 60.31, "description": "one large strawberry and chocolate cake" } { "_id": 4, "date": "2022-01-15", "orderTotal": 43.62, "description": "2 chicken lunches and a diet coke" } { "_id": 3, "date": "2022-01-11", "orderTotal": 19.49, "description": "1 dozen strawberry cupcakes" } { "_id": 2, "date": "2022-01-11", "orderTotal": 83.87, "description": "two medium strawberry birthday cakes" } { "_id": 1, "date": "2022-01-03", "orderTotal": 17.86, "description": "1/2 lb cream cheese and 1 dozen bagels" }
텍스트 점수
검색 결과가 검색 string과 얼마나 일치하는지를 나타내는 값인 텍스트 점수를 기준으로 텍스트 쿼리 결과를 정렬할 수 있습니다. 텍스트 쿼리 의 텍스트 점수를 기준으로 정렬을 지정하려면 Sorts.metaTextScore() 정적 팩토리 메서드를 사용합니다.
자세한 내용은 Sorts 클래스 API 설명서를 참조하세요.