콘솔로 이동

Cloud Firestore로 데이터 정렬 및 제한

Cloud Firestore는 컬렉션에서 검색할 문서를 지정하는 강력한 쿼리 기능을 제공합니다. 이러한 쿼리는 데이터 가져오기에 설명된 대로 get() 또는 addSnapshotListener()와 함께 사용할 수도 있습니다.

데이터 정렬 및 제한

기본적으로 쿼리는 쿼리 조건에 맞는 모든 문서를 문서 ID에 따라 오름차순으로 검색합니다. orderBy()를 사용하여 데이터의 정렬 순서를 지정하고 limit()를 사용하여 검색된 문서 수를 제한할 수 있습니다.

예를 들어 알파벳순으로 처음 3개 도시를 쿼리하는 방법은 다음과 같습니다.

citiesRef.orderBy("name").limit(3)
Swift
citiesRef.order(by: "name").limit(to: 3)
Objective-C
[[citiesRef queryOrderedByField:@"name"] queryLimitedTo:3];
  
자바
citiesRef.orderBy("name").limit(3);
Kotlin
citiesRef.orderBy("name").limit(3)
자바
Query query = cities.orderBy("name").limit(3);
Python
db.collection(u'cities').order_by(u'name').limit(3).stream()
Node.js
let firstThree = citiesRef.orderBy('name').limit(3);
Go
query := cities.OrderBy("name", firestore.Asc).Limit(3)
PHP
$query = $citiesRef->orderBy('name')->limit(3);
C#
Query query = citiesRef.OrderBy("Name").Limit(3);
Ruby
query = cities_ref.order("name").limit(3)

내림차순으로 정렬하여 마지막 3개 도시를 가져올 수도 있습니다.

citiesRef.orderBy("name", "desc").limit(3)
Swift
citiesRef.order(by: "name", descending: true).limit(to: 3)
Objective-C
[[citiesRef queryOrderedByField:@"name" descending:YES] queryLimitedTo:3];
  
자바
citiesRef.orderBy("name", Direction.DESCENDING).limit(3);
Kotlin
citiesRef.orderBy("name", Query.Direction.DESCENDING).limit(3)
자바
Query query = cities.orderBy("name", Direction.DESCENDING).limit(3);
Python
cities_ref = db.collection(u'cities')
query = cities_ref.order_by(
    u'name', direction=firestore.Query.DESCENDING).limit(3)
results = query.stream()
Node.js
let lastThree = citiesRef.orderBy('name', 'desc').limit(3);
Go
query := cities.OrderBy("name", firestore.Desc).Limit(3)
PHP
$query = $citiesRef->orderBy('name', 'DESC')->limit(3);
C#
Query query = citiesRef.OrderByDescending("Name").Limit(3);
Ruby
query = cities_ref.order("name", "desc").limit(3)

여러 필드를 기준으로 정렬할 수도 있습니다. 예를 들어 주에 따라 정렬한 후 각 주 안에서 인구에 따라 내림차순으로 정렬하는 방법은 다음과 같습니다.

citiesRef.orderBy("state").orderBy("population", "desc")
Swift
citiesRef
    .order(by: "state")
    .order(by: "population", descending: true)
Objective-C
[[citiesRef queryOrderedByField:@"state"] queryOrderedByField:@"population" descending:YES];
  
자바
citiesRef.orderBy("state").orderBy("population", Direction.DESCENDING);
Kotlin
citiesRef.orderBy("state").orderBy("population", Query.Direction.DESCENDING)
자바
Query query = cities.orderBy("state").orderBy("population", Direction.DESCENDING);
Python
cities_ref = db.collection(u'cities')
cities_ref.order_by(u'state').order_by(
    u'population', direction=firestore.Query.DESCENDING)
Node.js
let byStateByPop = citiesRef.orderBy('state').orderBy('population', 'desc');
Go
query := client.Collection("cities").OrderBy("state", firestore.Asc).OrderBy("population", firestore.Desc)
PHP
$query = $citiesRef->orderBy('state')->orderBy('population', 'DESC');
C#
Query query = citiesRef.OrderBy("State").OrderByDescending("Population");
Ruby
query = cities_ref.order("state").order("population", "desc")

where() 필터를 orderBy()limit()와 결합할 수 있습니다. 다음 예의 쿼리에서는 인구 기준을 정의하고, 인구를 오름차순으로 정렬하고, 기준을 초과하는 처음 몇 개의 결과만 반환합니다.

citiesRef.where("population", ">", 100000).orderBy("population").limit(2)
Swift
citiesRef
    .whereField("population", isGreaterThan: 100000)
    .order(by: "population")
    .limit(to: 2)
Objective-C
[[[citiesRef queryWhereField:@"population" isGreaterThan:@100000]
    queryOrderedByField:@"population"]
    queryLimitedTo:2];
  
자바
citiesRef.whereGreaterThan("population", 100000).orderBy("population").limit(2);
Kotlin
citiesRef.whereGreaterThan("population", 100000).orderBy("population").limit(2)
자바
Query query = cities.whereGreaterThan("population", 2500000L).orderBy("population").limit(2);
Python
cities_ref = db.collection(u'cities')
query = cities_ref.where(
    u'population', u'>', 2500000).order_by(u'population').limit(2)
results = query.stream()
Node.js
let biggest = citiesRef.where('population', '>', 2500000)
  .orderBy('population').limit(2);
Go
query := cities.Where("population", ">", 2500000).OrderBy("population", firestore.Desc).Limit(2)
PHP
$query = $citiesRef
    ->where('population', '>', 2500000)
    ->orderBy('population')
    ->limit(2);
C#
Query query = citiesRef
    .WhereGreaterThan("Population", 2500000)
    .OrderBy("Population")
    .Limit(2);
Ruby
query = cities_ref.where("population", ">", 2_500_000).order("population").limit(2)

그러나 필터에 범위 비교(<, <=, >, >=)가 포함된 경우 동일한 필드를 기준으로 1차 정렬이 이루어져야 합니다.

올바른 방법: 동일한 필드에 범위 필터 및 orderBy 사용

citiesRef.where("population", ">", 100000).orderBy("population")
Swift
citiesRef
    .whereField("population", isGreaterThan: 100000)
    .order(by: "population")
Objective-C
[[citiesRef queryWhereField:@"population" isGreaterThan:@100000]
    queryOrderedByField:@"population"];
  
자바
citiesRef.whereGreaterThan("population", 100000).orderBy("population");
Kotlin
citiesRef.whereGreaterThan("population", 100000).orderBy("population")
자바
Query query = cities.whereGreaterThan("population", 2500000L).orderBy("population");
Python
cities_ref = db.collection(u'cities')
query = cities_ref.where(
    u'population', u'>', 2500000).order_by(u'population')
results = query.stream()
Node.js
citiesRef.where('population', '>', 2500000).orderBy('population');
Go
query := cities.Where("population", ">", 2500000).OrderBy("population", firestore.Asc)
PHP
$query = $citiesRef
    ->where('population', '>', 2500000)
    ->orderBy('population');
C#
Query query = citiesRef
    .WhereGreaterThan("Population", 2500000)
    .OrderBy("Population");
Ruby
query = cities_ref.where("population", ">", 2_500_000).order("population")

잘못된 방법: 서로 다른 필드에 범위 필터 및 1차 orderBy 사용

citiesRef.where("population", ">", 100000).orderBy("country")
Swift
citiesRef
    .whereField("population", isGreaterThan: 100000)
    .order(by: "country")
Objective-C
[[citiesRef queryWhereField:@"population" isGreaterThan:@100000] queryOrderedByField:@"country"];
  
자바
citiesRef.whereGreaterThan("population", 100000).orderBy("country");
Kotlin
citiesRef.whereGreaterThan("population", 100000).orderBy("country")
자바
Query query = cities.whereGreaterThan("population", 2500000L).orderBy("country");
Python
cities_ref = db.collection(u'cities')
query = cities_ref.where(u'population', u'>', 2500000).order_by(u'country')
results = query.stream()
Node.js
citiesRef.where('population', '>', 2500000).orderBy('country');
Go
// Note: This is an invalid query. It violates the constraint that range
// and order by are required to be on the same field.
query := cities.Where("population", ">", 2500000).OrderBy("country", firestore.Asc)
PHP
$query = $citiesRef
    ->where('population', '>', 2500000)
    ->orderBy('country');
C#
Query query = citiesRef
    .WhereGreaterThan("Population", 2500000)
    .OrderBy("Country");
Ruby
query = cities_ref.where("population", ">", 2_500_000).order("country")