Cloud Firestore มีฟังก์ชันการสืบค้นที่มีประสิทธิภาพสำหรับการระบุเอกสารที่คุณต้องการดึงจากคอลเล็กชัน แบบสอบถามเหล่านี้ยังสามารถใช้กับ get()
หรือ addSnapshotListener()
ตามที่อธิบายไว้ใน Get Data
สั่งซื้อและจำกัดข้อมูล
ตามค่าเริ่มต้น คิวรีจะดึงเอกสารทั้งหมดที่ตรงกับคิวรีในลำดับจากน้อยไปมากตามรหัสเอกสาร คุณสามารถระบุลำดับการจัดเรียงสำหรับข้อมูลของคุณโดยใช้ 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);
Swift
citiesRef.order(by: "name").limit(to: 3)
วัตถุประสงค์-C
[[citiesRef queryOrderedByField:@"name"] queryLimitedTo:3];
Java
citiesRef.orderBy("name").limit(3);
Kotlin+KTX
citiesRef.orderBy("name").limit(3)
Dart
final citiesRef = db.collection("cities"); citiesRef.orderBy("name").limit(3);
Java
Python
Python
C++
cities_ref.OrderBy("name").Limit(3);
Node.js
ไป
PHP
$query = $citiesRef->orderBy('name')->limit(3);
ความสามัคคี
Query query = citiesRef.OrderBy("Name").Limit(3);
ค#
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);
Swift
citiesRef.order(by: "name", descending: true).limit(to: 3)
วัตถุประสงค์-C
[[citiesRef queryOrderedByField:@"name" descending:YES] queryLimitedTo:3];
Java
citiesRef.orderBy("name", Direction.DESCENDING).limit(3);
Kotlin+KTX
citiesRef.orderBy("name", Query.Direction.DESCENDING).limit(3)
Dart
final citiesRef = db.collection("cities"); citiesRef.orderBy("name", descending: true).limit(3);
Java
Python
Python
C++
cities_ref.OrderBy("name", Query::Direction::kDescending).Limit(3);
Node.js
ไป
PHP
$query = $citiesRef->orderBy('name', 'DESC')->limit(3);
ความสามัคคี
Query query = citiesRef.OrderByDescending("Name").Limit(3);
ค#
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");
Swift
citiesRef .order(by: "state") .order(by: "population", descending: true)
วัตถุประสงค์-C
[[citiesRef queryOrderedByField:@"state"] queryOrderedByField:@"population" descending:YES];
Java
citiesRef.orderBy("state").orderBy("population", Direction.DESCENDING);
Kotlin+KTX
citiesRef.orderBy("state").orderBy("population", Query.Direction.DESCENDING)
Dart
final citiesRef = db.collection("cities"); citiesRef.orderBy("state").orderBy("population", descending: true);
Java
Python
Python
C++
cities_ref.OrderBy("state").OrderBy("name", Query::Direction::kDescending);
Node.js
ไป
PHP
$query = $citiesRef->orderBy('state')->orderBy('population', 'DESC');
ความสามัคคี
Query query = citiesRef.OrderBy("State").OrderByDescending("Population");
ค#
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);
Swift
citiesRef .whereField("population", isGreaterThan: 100000) .order(by: "population") .limit(to: 2)
วัตถุประสงค์-C
[[[citiesRef queryWhereField:@"population" isGreaterThan:@100000] queryOrderedByField:@"population"] queryLimitedTo:2];
Java
citiesRef.whereGreaterThan("population", 100000).orderBy("population").limit(2);
Kotlin+KTX
citiesRef.whereGreaterThan("population", 100000).orderBy("population").limit(2)
Dart
final citiesRef = db.collection("cities"); citiesRef .where("population", isGreaterThan: 100000) .orderBy("population") .limit(2);
Java
Python
Python
C++
cities_ref.WhereGreaterThan("population", FieldValue::Integer(100000)) .OrderBy("population") .Limit(2);
Node.js
ไป
PHP
$query = $citiesRef ->where('population', '>', 2500000) ->orderBy('population') ->limit(2);
ความสามัคคี
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Population") .Limit(2);
ค#
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");
Swift
หมายเหตุ: ผลิตภัณฑ์นี้ไม่สามารถใช้ได้กับเป้าหมาย watchOS และ App ClipcitiesRef .whereField("population", isGreaterThan: 100000) .order(by: "population")
วัตถุประสงค์-C
หมายเหตุ: ผลิตภัณฑ์นี้ไม่สามารถใช้ได้กับเป้าหมาย watchOS และ App Clip[[citiesRef queryWhereField:@"population" isGreaterThan:@100000] queryOrderedByField:@"population"];
Java
citiesRef.whereGreaterThan("population", 100000).orderBy("population");
Kotlin+KTX
citiesRef.whereGreaterThan("population", 100000).orderBy("population")
Dart
final citiesRef = db.collection("cities"); citiesRef.where("population", isGreaterThan: 100000).orderBy("population");
Java
Query query = cities.whereGreaterThan("population", 2500000L).orderBy("population");
Python
Python
Node.js
ไป
PHP
$query = $citiesRef ->where('population', '>', 2500000) ->orderBy('population');
ความสามัคคี
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Population");
ค#
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");
Swift
หมายเหตุ: ผลิตภัณฑ์นี้ไม่สามารถใช้ได้กับเป้าหมาย watchOS และ App ClipcitiesRef .whereField("population", isGreaterThan: 100000) .order(by: "country")
วัตถุประสงค์-C
หมายเหตุ: ผลิตภัณฑ์นี้ไม่สามารถใช้ได้กับเป้าหมาย watchOS และ App Clip[[citiesRef queryWhereField:@"population" isGreaterThan:@100000] queryOrderedByField:@"country"];
Java
citiesRef.whereGreaterThan("population", 100000).orderBy("country");
Kotlin+KTX
citiesRef.whereGreaterThan("population", 100000).orderBy("country")
Dart
final citiesRef = db.collection("cities"); citiesRef.where("population", isGreaterThan: 100000).orderBy("country");
Java
Python
Python
C++
// BAD EXAMPLE -- will crash the program: cities_ref.WhereGreaterThan("population", FieldValue::Integer(100000)) .OrderBy("country");
Node.js
ไป
PHP
$invalidRangeQuery = $citiesRef ->where('population', '>', 2500000) ->orderBy('country');
ความสามัคคี
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Country");
ค#
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Country");
ทับทิม
- คุณไม่สามารถเรียงลำดับการสืบค้นของคุณโดยใช้ฟิลด์ใดๆ ที่รวมอยู่ในความเท่าเทียมกัน (
=
) หรือin
อนุประโยค