설명
하나 이상의 지정된 정렬 순서에 따라 입력 문서를 정렬합니다.
구문
다음 예에서는 cities 컬렉션을 population에 따라 오름차순으로 정렬합니다.
Node.js
const results = await db.pipeline()
.collection("/cities")
.sort(Field.of("population").ascending())
.execute();
다음 예에서는 cities 컬렉션을 도시 이름의 length에 따라 오름차순으로 정렬합니다.
Node.js
const results = await db.pipeline()
.collection("/cities")
.sort(Field.of("name").charLength().ascending())
.execute();
클라이언트 예시
Web
const results = await execute(db.pipeline() .collection("books") .sort( field("release_date").descending(), field("author").ascending() ) );
Swift
let results = try await db.pipeline() .collection("books") .sort([ Field("release_date").descending(), Field("author").ascending() ]) .execute()
Kotlin
val results = db.pipeline() .collection("books") .sort( field("release_date").descending(), field("author").ascending() ) .execute()
Java
Task<Pipeline.Snapshot> results = db.pipeline() .collection("books") .sort( field("release_date").descending(), field("author").ascending() ) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field results = ( client.pipeline() .collection("books") .sort(Field.of("release_date").descending(), Field.of("author").ascending()) .execute() )
자바
Pipeline.Snapshot results = firestore .pipeline() .collection("books") .sort(descending(field("release_date")), ascending(field("author"))) .execute() .get();
동작
정렬 순서
정렬 순서는 Cloud Firestore의 값 유형 순서를 따릅니다.
결과의 결정적 순서
쿼리에 sort 단계가 없으면 반환된 결과의 순서가 비결정론적이며 실행 간에 다를 수 있습니다. sort 단계가 있지만 정렬 표현식이 반환된 결과 간에 고유한 정렬을 생성하지 못하는 경우 반환된 결과의 정렬은 실행 간에 여전히 다를 수 있습니다.
예를 들어 다음 데이터 세트에 대해 도시의 country를 기준으로 도시를 오름차순으로 정렬하면
{name: 'Los Angeles', state: 'CA', country: 'USA', population: 3970000},
{name: 'New York', state: 'NY', country: 'USA', population: 8530000}
{name: 'San Francisco', state: 'CA', country: 'USA', population: 870000},
모두 동일한 country 속성을 가지고 있으므로 데이터 세트의 3개 문서의 순열을 생성할 수 있습니다. 결과의 결정적 순서를 생성하려면 name의 정렬 순서를 순서에 추가하면 됩니다.
Node.js
const results = await db.pipeline()
.collection("/cities")
.sort(Field.of("country").ascending(), Field.of("__name__").ascending())
.execute();
이렇게 하면 여러 문서의 country 값이 동일한 경우 고유한 문서 이름을 결정자로 사용합니다. country 필드와 함께 컬렉션 내 문서의 고유 키를 형성하는 다른 필드를 사용하여 결과의 결정적 순서를 생성할 수 있습니다.
동등한 값 정렬
동등한 값은 함께 정렬되지만 동등한 클래스 내 결과의 순서는 결과의 결정적 순서 토론에 따라 결정적이지 않습니다. 예를 들어 다음 데이터 세트에 대해 size를 기준으로 도시를 오름차순으로 정렬하면
{name: 'Los Angeles', state: 'CA', country: 'USA', size: 3970000},
{name: 'Mexico City', state: null, country: 'Mexico', size: 3970000.0},
두 문서 모두 동등한 size 값 3970000을 가지므로 데이터 세트에서 두 문서의 순열을 생성할 수 있습니다.
여러 정렬 단계
쿼리에 연속된 정렬 단계가 여러 개 포함된 경우 마지막 정렬 단계만 쿼리 결과에 영향을 미칩니다. 이는 Core API의 orderBy 절의 동작과 다릅니다.
상위 N개 정렬 최적화
sort 뒤에 limit이 사용되면 상위 N개 정렬이 사용될 수 있습니다. 이 최적화는 limit에 정의된 대로 한 번에 N개의 문서만 저장하도록 허용하여 정렬 단계의 메모리 사용량을 제한하므로 정렬이 메모리 효율적으로 이루어집니다.
Null 및 Absent 값
정렬에 지정된 필드가 문서에 없으면 값이 null인 것처럼 정렬됩니다.
예를 들어 다음 데이터 세트에 대해 도시의 state를 기준으로 도시를 오름차순으로 정렬하면
{name: 'Los Angeles', state: 'CA', country: 'USA', population: 3970000},
{name: 'Mexico City', state: null, country: 'Mexico', population: 9200000},
{name: 'New York', state: 'NY', country: 'USA', population: 8530000}
{name: 'San Francisco', state: 'CA', country: 'USA', population: 870000},
{name: 'Toronto', country: 'Canada', population: 2930000},
'Toronto' 문서와 'Mexico City' 문서가 null로 정렬되고 다른 문서보다 앞에 오는 다음 결과를 생성합니다.
{name: 'Toronto', country: 'Canada', population: 2930000},
{name: 'Mexico City', state: null, country: 'Mexico', population: 9200000},
{name: 'Los Angeles', state: 'CA', country: 'USA', population: 3970000},
{name: 'San Francisco', state: 'CA', country: 'USA', population: 870000},
{name: 'New York', state: 'NY', country: 'USA', population: 8530000}