Cloud Firestore는 컬렉션에서 검색하려는 문서를 지정하기 위한 강력한 쿼리 기능을 제공합니다. 이러한 쿼리는 데이터 가져오기 에 설명된 대로 get()
또는 addSnapshotListener()
와 함께 사용할 수도 있습니다.
주문 및 제한 데이터
기본적으로 쿼리는 쿼리를 충족하는 모든 문서를 문서 ID별로 오름차순으로 검색합니다. orderBy()
를 사용하여 데이터의 정렬 순서를 지정할 수 있고 limit()
를 사용하여 검색되는 문서 수를 제한할 수 있습니다.
예를 들어 다음과 같이 사전순으로 처음 3개 도시를 쿼리할 수 있습니다.
Web version 9
import { query, orderBy, limit } from "firebase/firestore"; const q = query(citiesRef, orderBy("name"), limit(3));
Web version 8
citiesRef.orderBy("name").limit(3);
빠른
citiesRef.order(by: "name").limit(to: 3)
목표-C
[[citiesRef queryOrderedByField:@"name"] queryLimitedTo:3];
Kotlin+KTX
citiesRef.orderBy("name").limit(3)
Java
citiesRef.orderBy("name").limit(3);
Dart
final citiesRef = db.collection("cities"); citiesRef.orderBy("name").limit(3);
자바
파이썬
Python
C++
cities_ref.OrderBy("name").Limit(3);
Node.js
가다
PHP
PHP
Cloud Firestore 클라이언트 설치 및 생성에 대한 자세한 내용은 Cloud Firestore 클라이언트 라이브러리 를 참조하세요.
단일성
Query query = citiesRef.OrderBy("Name").Limit(3);
씨#
루비
내림차순으로 정렬하여 마지막 3개 도시를 가져올 수도 있습니다.
Web version 9
import { query, orderBy, limit } from "firebase/firestore"; const q = query(citiesRef, orderBy("name", "desc"), limit(3));
Web version 8
citiesRef.orderBy("name", "desc").limit(3);
빠른
citiesRef.order(by: "name", descending: true).limit(to: 3)
목표-C
[[citiesRef queryOrderedByField:@"name" descending:YES] queryLimitedTo:3];
Kotlin+KTX
citiesRef.orderBy("name", Query.Direction.DESCENDING).limit(3)
Java
citiesRef.orderBy("name", Direction.DESCENDING).limit(3);
Dart
final citiesRef = db.collection("cities"); citiesRef.orderBy("name", descending: true).limit(3);
자바
파이썬
Python
C++
cities_ref.OrderBy("name", Query::Direction::kDescending).Limit(3);
Node.js
가다
PHP
PHP
Cloud Firestore 클라이언트 설치 및 생성에 대한 자세한 내용은 Cloud Firestore 클라이언트 라이브러리 를 참조하세요.
단일성
Query query = citiesRef.OrderByDescending("Name").Limit(3);
씨#
루비
여러 필드로 주문할 수도 있습니다. 예를 들어 주별로 정렬하고 각 주 내에서 인구별로 내림차순으로 정렬하려는 경우:
Web version 9
import { query, orderBy } from "firebase/firestore"; const q = query(citiesRef, orderBy("state"), orderBy("population", "desc"));
Web version 8
citiesRef.orderBy("state").orderBy("population", "desc");
빠른
citiesRef .order(by: "state") .order(by: "population", descending: true)
목표-C
[[citiesRef queryOrderedByField:@"state"] queryOrderedByField:@"population" descending:YES];
Kotlin+KTX
citiesRef.orderBy("state").orderBy("population", Query.Direction.DESCENDING)
Java
citiesRef.orderBy("state").orderBy("population", Direction.DESCENDING);
Dart
final citiesRef = db.collection("cities"); citiesRef.orderBy("state").orderBy("population", descending: true);
자바
파이썬
Python
C++
cities_ref.OrderBy("state").OrderBy("name", Query::Direction::kDescending);
Node.js
가다
PHP
PHP
Cloud Firestore 클라이언트 설치 및 생성에 대한 자세한 내용은 Cloud Firestore 클라이언트 라이브러리 를 참조하세요.
단일성
Query query = citiesRef.OrderBy("State").OrderByDescending("Population");
씨#
루비
where()
필터를 orderBy()
및 limit()
) 와 결합할 수 있습니다. 다음 예에서 쿼리는 모집단 임계값을 정의하고 모집단을 오름차순으로 정렬하고 임계값을 초과하는 처음 몇 개의 결과만 반환합니다.
Web version 9
import { query, where, orderBy, limit } from "firebase/firestore"; const q = query(citiesRef, where("population", ">", 100000), orderBy("population"), limit(2));
Web version 8
citiesRef.where("population", ">", 100000).orderBy("population").limit(2);
빠른
citiesRef .whereField("population", isGreaterThan: 100000) .order(by: "population") .limit(to: 2)
목표-C
[[[citiesRef queryWhereField:@"population" isGreaterThan:@100000] queryOrderedByField:@"population"] queryLimitedTo:2];
Kotlin+KTX
citiesRef.whereGreaterThan("population", 100000).orderBy("population").limit(2)
Java
citiesRef.whereGreaterThan("population", 100000).orderBy("population").limit(2);
Dart
final citiesRef = db.collection("cities"); citiesRef .where("population", isGreaterThan: 100000) .orderBy("population") .limit(2);
자바
파이썬
Python
C++
cities_ref.WhereGreaterThan("population", FieldValue::Integer(100000)) .OrderBy("population") .Limit(2);
Node.js
가다
PHP
PHP
Cloud Firestore 클라이언트 설치 및 생성에 대한 자세한 내용은 Cloud Firestore 클라이언트 라이브러리 를 참조하세요.
단일성
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Population") .Limit(2);
씨#
루비
그러나 범위 비교( <
, <=
, >
, >=
)가 포함된 필터가 있는 경우 첫 번째 정렬은 동일한 필드에 있어야 합니다. 아래 orderBy()
제한 목록을 참조하세요.
제한 사항
orderBy()
절에 대한 다음 제한 사항에 유의하십시오.
- 또한
orderBy()
절은 주어진 필드의 존재 여부를 필터링합니다. 결과 세트는 지정된 필드를 포함하지 않는 문서를 포함하지 않습니다. 범위 비교(
<
,<=
,>
,>=
)가 포함된 필터를 포함하는 경우 첫 번째 정렬은 동일한 필드에 있어야 합니다.유효한 : 동일한 필드의 범위 필터 및
orderBy
Web version 9
import { query, where, orderBy } from "firebase/firestore"; const q = query(citiesRef, where("population", ">", 100000), orderBy("population"));
Web version 8
citiesRef.where("population", ">", 100000).orderBy("population");
빠른
참고: 이 제품은 watchOS 및 App Clip 대상에서 사용할 수 없습니다.citiesRef .whereField("population", isGreaterThan: 100000) .order(by: "population")
목표-C
참고: 이 제품은 watchOS 및 App Clip 대상에서 사용할 수 없습니다.[[citiesRef queryWhereField:@"population" isGreaterThan:@100000] queryOrderedByField:@"population"];
Kotlin+KTX
citiesRef.whereGreaterThan("population", 100000).orderBy("population")
Java
citiesRef.whereGreaterThan("population", 100000).orderBy("population");
Dart
final citiesRef = db.collection("cities"); citiesRef.where("population", isGreaterThan: 100000).orderBy("population");
자바
Query query = cities.whereGreaterThan("population", 2500000L).orderBy("population");
파이썬
Python
Node.js
가다
PHP
PHP
Cloud Firestore 클라이언트 설치 및 생성에 대한 자세한 내용은 Cloud Firestore 클라이언트 라이브러리 를 참조하세요.
단일성
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Population");
씨#
루비
유효 하지 않음: 다른 필드의 범위 필터 및 첫 번째
orderBy
Web version 9
import { query, where, orderBy } from "firebase/firestore"; const q = query(citiesRef, where("population", ">", 100000), orderBy("country"));
Web version 8
citiesRef.where("population", ">", 100000).orderBy("country");
빠른
참고: 이 제품은 watchOS 및 App Clip 대상에서 사용할 수 없습니다.citiesRef .whereField("population", isGreaterThan: 100000) .order(by: "country")
목표-C
참고: 이 제품은 watchOS 및 App Clip 대상에서 사용할 수 없습니다.[[citiesRef queryWhereField:@"population" isGreaterThan:@100000] queryOrderedByField:@"country"];
Kotlin+KTX
citiesRef.whereGreaterThan("population", 100000).orderBy("country")
Java
citiesRef.whereGreaterThan("population", 100000).orderBy("country");
Dart
final citiesRef = db.collection("cities"); citiesRef.where("population", isGreaterThan: 100000).orderBy("country");
자바
파이썬
Python
C++
// BAD EXAMPLE -- will crash the program: cities_ref.WhereGreaterThan("population", FieldValue::Integer(100000)) .OrderBy("country");
Node.js
가다
PHP
PHP
Cloud Firestore 클라이언트 설치 및 생성에 대한 자세한 내용은 Cloud Firestore 클라이언트 라이브러리 를 참조하세요.
단일성
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Country");
씨#
루비
- 같음(
=
) 또는in
절에 포함된 필드별로 쿼리를 정렬할 수 없습니다.