데이터 정렬 및 제한

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

데이터 정렬 및 제한

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

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

웹 버전 9

import { query, orderBy, limit } from "firebase/firestore";  

const q = query(citiesRef, orderBy("nam);order_and_limit.js

웹 버전 8

citiesRef.orderBy("name")restore.js
Swift
참고: 이 제품은 watchOS 및 앱 클립 대상에서 사용할 수 없습니다.
citiesRef.order(by: "name").liller.swift
Objective-C
참고: 이 제품은 watchOS 및 앱 클립 대상에서 사용할 수 없습니다.
[[citiesRef queryOrderedByField:@"name"] queryLimntroller.m
Kotlin
Android
citiesRef.orderBy("name"nippets.kt
자바
Android
citiesRef.orderBy("name")ppets.java

Dart

final citiesRef = db.collection("cities");
citiesRef.orderBy("nit(3);firestore.dart
자바
Query query = cities.orderBy("name").limit(3);
Query query = cities.orderBy("name&queryDataSnippets.java
Python
cities_ref = db.collection("cities")
query = cities_ref.order_by("name").limit_to_last(2)
rery.get()snippets.py
Python
(비동기)
cities_ref = db.collection("cities")
query = cities_ref.order_by("name").limit_to_last(2)
resultsery.get()snippets.py
C++
cities_ref.OrderBy("name")ippets.cpp
Node.js
const firstThreeRes = await citiesRef.orderBy('name').limit(3index.js
Go
query := cities.OrderBy("name", firestore.Asc).Limit(3)
query := cities.OrderBy("name", firestoremitToLast(3)query.go
PHP

PHP

Firestore에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

$query = $citiesRef->orderBy('nam>e')-limit(3);
Unity
Query query = citiesRef.OrderBy("Name").Limit(3);
C#
Query query = citiesRef.OrderBy("Name")Program.cs
Ruby
query = cities_ref.order("name"it_data.rb

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

웹 버전 9

import { query, orderBy, limit } from "firebase/firestore";  

const q = query(citiesRef, orderBy("name",it(3));order_and_limit_desc.js

웹 버전 8

citiesRef.orderBy("name", "d3);test.firestore.js
Swift
참고: 이 제품은 watchOS 및 앱 클립 대상에서 사용할 수 없습니다.
citiesRef.order(by: "name", descending: true).liller.swift
Objective-C
참고: 이 제품은 watchOS 및 앱 클립 대상에서 사용할 수 없습니다.
[[citiesRef queryOrderedByField:@"name" descending:YES] queryLimntroller.m
Kotlin
Android
citiesRef.orderBy("name", Query.Direction.DESCENDINGnippets.kt
자바
Android
citiesRef.orderBy("name", Direction.DESCENDING)ppets.java

Dart

final citiesRef = db.collection("cities");
citiesRef.orderBy("name", descendit(3);firestore.dart
자바
Query query = cities.orderBy("name", Direction.DESCENDING)ppets.java
Python
cities_ref = db.collection("cities")
query = cities_ref.order_by("name", direction=firestore.Query.DESCENDING).limit(3)
resu.stream()snippets.py
Python
(비동기)
cities_ref = db.collection("cities")
query = cities_ref.order_by("name", direction=firestore.Query.DESCENDING).limit(3)
resu.stream()snippets.py
C++
cities_ref.OrderBy("name", Query::Direction::kDescending)ippets.cpp
Node.js
const lastThreeRes = await citiesRef.orderBy('name', 'desc')).get();index.js
Go
query := cities.OrderBy("name", firestore.Desc3)query.go
PHP

PHP

Firestore에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

$query = $citiesRef->orderBy('name', >'DESC')-limit(3);
Unity
Query query = citiesRef.OrderByDescending("Name").Limit(3);
C#
Query query = citiesRef.OrderByDescending("Name")Program.cs
Ruby
query = cities_ref.order("name", ")order_limit_data.rb

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

웹 버전 9

import { query, orderBy } from "firebase/firestore";  

const q = query(citiesRef, orderBy("state"), orderBy(&qut;, "desc"));order_multiple.js

웹 버전 8

citiesRef.orderBy("state").orderBy("popula;desc");test.firestore.js
Swift
참고: 이 제품은 watchOS 및 앱 클립 대상에서 사용할 수 없습니다.
citiesRef
  .order(by: "state")
  .order(by: "population"ViewController.swift
Objective-C
참고: 이 제품은 watchOS 및 앱 클립 대상에서 사용할 수 없습니다.
[[citiesRef queryOrderedByField:@"state"] queryOrderedByField:@"population&quES];ViewController.m
Kotlin
Android
citiesRef.orderBy("state").orderBy("population", Query.DNDING)DocSnippets.kt
자바
Android
citiesRef.orderBy("state").orderBy("population", DiNG);DocSnippets.java

Dart

final citiesRef = db.collection("cities");
citiesRef.orderBy("state").orderBy("populascending: true);firestore.dart
자바
Query query = cities.orderBy("state").orderBy("population", DieryDataSnippets.java
Python
cities_ref = db.collection("cities")
ordered_city_ref = cities_ref.order_by("state").order_by(
    "population", directio.Query.DESCENDING
)snippets.py
Python
(비동기)
cities_ref = db.collection("cities")
cities_ref.order_by("state").order_by(
    "population", directio.Query.DESCENDING
)snippets.py
C++
cities_ref.OrderBy("state").OrderBy("name", Query::Direending);snippets.cpp
Node.js
const byStateByPopRes = await citiesRef.orderBy('state').orderBy('population'esc').get();index.js
Go
query := client.Collection("cities").OrderBy("state", firestore.Asc).OrderBy("popquot;, firestore.Desc)query.go
PHP

PHP

Firestore에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

$query = $citiesRef->orderBy('stat>e')-orderBy('population', 'DESC');
Unity
Query query = citiesRef.OrderBy("State").OrderByDescending("Population");
C#
Query query = citiesRef.OrderBy("State").OrderByDescending(&quoon");Program.cs
Ruby
query = cities_ref.order("state").order("populdesc")order_limit_data.rb

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

웹 버전 9

import { query, where, orderBy, limit } from "firebase/firestore";  

const q = query(citiesRef, where(">;population", "", 100000), ordon"), limit(2));filter_and_order.js

웹 버전 8

citiesRef.where("populatio>n", "", 100000).orderBy(&qut;).limit(2);test.firestore.js
Swift
참고: 이 제품은 watchOS 및 앱 클립 대상에서 사용할 수 없습니다.
citiesRef
  .whereField("population", isGreaterThan: 100000)
  .order(by: "population&quViewController.swift
Objective-C
참고: 이 제품은 watchOS 및 앱 클립 대상에서 사용할 수 없습니다.
[[[citiesRef queryWhereField:@"population" isGreaterThan:@100000]
    queryOrderedByField:@"population"]
  :2];ViewController.m
Kotlin
Android
citiesRef.whereGreaterThan("population", 100000).orderBy("populamit(2)DocSnippets.kt
자바
Android
citiesRef.whereGreaterThan("population", 100000).orderBy("populat(2);DocSnippets.java

Dart

final citiesRef = db.collection("cities");
citiesRef
    .where("population", isGreaterThan: 100000)
    .orderBy("po)
    .limit(2);firestore.dart
자바
Query query = cities.whereGreaterThan("population", 2500000L).orderBy("populateryDataSnippets.java
Python
cities_ref = db.collection("cities")
query = (
    cities_ref.where(filter=FieldFilter(">;population", "", 2500000))
    .order_by("population")
2)
)
results = query.stream()snippets.py
Python
(비동기)
cities_ref = db.collection("cities")
query = (
    cities_ref.where(filter=FieldFilter(">;population", "", 2500000))
    .order_by("population")
2)
)
results = query.stream()snippets.py
C++
cities_ref.WhereGreaterThan("population", FieldValue::Integer(100000))
    .OrderBy("population&qimit(2);snippets.cpp
Node.js
const biggestRes = await citiesRef.where('population>', '', 2500000)
  .orderBy('populati.limit(2).get();index.js
Go
query := cities.Where("populatio>n", "", 2500000).OrderBy("population&qrestore.Desc).Limit(2)query.go
PHP

PHP

Firestore에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

$query = $citiesRef
    ->where('population>', ''>, 2500000)
    -orderBy(>9;population')
    -limit(2);
Unity
Query query = citiesRef
    .WhereGreaterThan("Population", 2500000)
    .OrderBy("Population")
    .Limit(2);
C#
Query query = citiesRef
    .WhereGreaterThan("Population", 2500000)
    .OrderBy("Population&q.Limit(2);Program.cs
Ruby
query = cities_ref.where("populatio>n", "", 2_500_000).order(&q;).limit(2)order_limit_data.rb

그러나 필터에 범위 비교(<, <=, >, >=)가 포함된 경우 동일한 필드를 기준으로 1차 정렬이 이루어져야 합니다. 아래의 orderBy() 제한사항 목록을 참조하세요.

제한사항

orderBy() 절에는 다음과 같은 제한사항이 있습니다.

orderBy 및 존재 여부

특정 필드를 기준으로 쿼리를 정렬할 때 쿼리는 정렬 기준 필드가 있는 문서만 반환할 수 있습니다.

예를 들어 다음 쿼리는 쿼리 필터가 충족되더라도 population 필드가 설정되지 않은 문서를 반환하지 않습니다.

Java
db.collection("cities").whereEqualTo("country", USA).orderBy(population);

관련 효과는 부등식에 적용됩니다. 필드에 부등식 필터가 있는 쿼리는 해당 필드를 기준으로 한 정렬도 포함합니다. 다음 쿼리는 해당 문서에 country = USA가 있더라도 population 필드가 없는 문서를 반환하지 않습니다 . 이 문제를 해결하려면 주문마다 별도의 쿼리를 실행하거나 정렬하는 모든 필드에 값을 할당할 수 있습니다.

Java
db.collection(cities).where(or(country, USA), greaterThan(population, 250000));

위 쿼리는 부등식에 대한 묵시적 정렬을 포함하며 다음과 같습니다.

Java
db.collection(cities).where(or(country, USA), greaterThan(population, 250000)).orderBy(population);