Docs Menu
Docs Home
/ / /
Java 동기화 드라이버
/

정렬 빌더

이 가이드에서는 MongoDB Java 드라이버의 빌더 를 사용하여 쿼리에 대한 정렬 기준 을 지정하는 방법에 대해 설명합니다.

정렬 기준은 MongoDB가 데이터를 정렬하는 데 사용하는 규칙입니다. 정렬 기준의 몇 가지 예는 다음과 같습니다:

  • 가장 작은 숫자에서 가장 큰 숫자로

  • 가장 이른 시간부터 가장 늦은 시간까지

  • 이름별 알파벳 순서

빌더는 BSON 객체를 구성하는 데 도움이 되는 MongoDB Java 드라이버에서 제공하는 클래스입니다. 자세한 내용은 빌더 가이드를 참조하세요.

빌더를 사용하여 쿼리에 대한 정렬 기준을 지정하려면 이 가이드를 읽어보세요.

MongoDB Java 드라이버에서 정렬의 기본 사항을 알아보려면 정렬에 대한 가이드를 참조하세요.

이 페이지의 예제에서는 다음 문서가 포함된 샘플 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 클래스는 MongoDB에서 지원하는 모든 정렬 기준 연산자에 대한 정적 팩토리 메서드를 제공하는 빌더입니다. 이러한 메서드는 FindIterable 인스턴스의 sort() 메서드 또는 Aggregates.sort() 에 전달할 수 있는 Bson 객체를 반환합니다. Aggregates 클래스에 대해 자세히 알아보려면 애그리게이션 빌더에 대한 가이드를 참조하세요.

이 섹션의 클래스 및 인터페이스에 대한 자세한 내용은 다음 API 설명서를 참조하세요.

  • 정렬

  • BSON

  • FindIterable

  • 집계

오름차순 정렬을 지정하려면 Sorts.ascending() 정적 팩토리 메서드를 사용합니다. 정렬할 필드의 이름을 Sorts.ascending() 에 전달합니다.

다음 예에서는 샘플 컬렉션 의 문서를 _id 필드에서 오름차순으로 정렬합니다.

import static com.mongodb.client.model.Sorts.ascending;
// <MongoCollection setup code here>
collection.find().sort(ascending("_id"));

앞의 예제의 출력은 다음과 유사합니다.

{ "_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 필드에서 내림차순으로 정렬합니다.

import static com.mongodb.client.model.Sorts.descending;
// <MongoCollection setup code here>
collection.find().sort(descending("_id"));

앞의 예제는 다음과 같이 출력되어야 합니다.

{ "_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 의 문서를 letter 필드에서는 내림차순으로 정렬하고, 동점인 경우 _id 필드에서는 오름차순으로 정렬합니다.

import static com.mongodb.client.model.Sorts.orderBy;
import static com.mongodb.client.model.Sorts.ascending;
import static com.mongodb.client.model.Sorts.descending;
// <MongoCollection setup code here>
Bson orderBySort = orderBy(descending("date"), ascending("orderTotal"));
collection.find().sort(orderBySort);

앞의 예제의 출력은 다음과 유사합니다.

{ "_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" }

검색 결과가 검색 문자열과 얼마나 일치하는지를 나타내는 값인 텍스트 점수를 기준으로 텍스트 쿼리 결과를 정렬할 수 있습니다. 텍스트 쿼리 의 텍스트 점수를 기준으로 정렬을 지정하려면 Sorts.metaTextScore() 정적 팩토리 메서드를 사용합니다. Sorts.metaTextScore() 메서드를 사용하여 정렬 기준을 지정하는 방법을 보여주는 자세한 예시는 정렬 기본 사항 가이드의 텍스트 쿼리 섹션을 참조하세요.

자세한 내용은 Sorts 클래스 API 설명서를 참조하세요.

돌아가기

프로젝션

이 페이지의 내용