Cloud Firestore มีฟังก์ชันการสืบค้นที่มีประสิทธิภาพสำหรับการระบุเอกสารที่ต้องการดึงข้อมูลจากคอลเล็กชัน นอกจากนี้ คุณยังใช้การค้นหาเหล่านี้กับ get()
หรือ addSnapshotListener()
ได้ด้วยตามที่อธิบายไว้ในรับข้อมูล
ข้อมูลคำสั่งซื้อและขีดจำกัด
โดยค่าเริ่มต้น การค้นหาจะดึงข้อมูลเอกสารทั้งหมดที่ตรงกับการค้นหาในลำดับจากน้อยไปมากตามรหัสเอกสาร คุณระบุลำดับการจัดเรียงข้อมูลได้โดยใช้
orderBy()
และจำกัดจำนวนเอกสารที่ดึงข้อมูลได้โดยใช้
limit()
หากระบุ limit()
ค่าต้องมากกว่าหรือเท่ากับ 0
เช่น คุณสามารถค้นหา 3 เมืองแรกตามลำดับตัวอักษรได้โดยใช้คำสั่งต่อไปนี้
Web
import { query, orderBy, limit } from "firebase/firestore"; const q = query(citiesRef, orderBy("name"), limit(3));
Web
citiesRef.orderBy("name").limit(3);
Swift
citiesRef.order(by: "name").limit(to: 3)
Objective-C
[[citiesRef queryOrderedByField:@"name"] queryLimitedTo:3];
Kotlin
citiesRef.orderBy("name").limit(3)
Java
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
Go
PHP
PHP
ดูข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งและสร้างCloud Firestoreไคลเอ็นต์ได้ที่ Cloud Firestoreไลบรารีของไคลเอ็นต์
Unity
Query query = citiesRef.OrderBy("Name").Limit(3);
C#
Ruby
คุณยังจัดเรียงจากมากไปน้อยเพื่อดู 3 เมืองล่าสุดได้ด้วย
Web
import { query, orderBy, limit } from "firebase/firestore"; const q = query(citiesRef, orderBy("name", "desc"), limit(3));
Web
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];
Kotlin
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);
Java
Python
Python
C++
cities_ref.OrderBy("name", Query::Direction::kDescending).Limit(3);
Node.js
Go
PHP
PHP
ดูข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งและสร้างCloud Firestoreไคลเอ็นต์ได้ที่ Cloud Firestoreไลบรารีของไคลเอ็นต์
Unity
Query query = citiesRef.OrderByDescending("Name").Limit(3);
C#
Ruby
นอกจากนี้ คุณยังจัดเรียงตามหลายฟิลด์ได้ด้วย ตัวอย่างเช่น หากต้องการจัดเรียงตามรัฐ และจัดเรียงประชากรภายในแต่ละรัฐตามลำดับจากมากไปน้อย ให้ทำดังนี้
Web
import { query, orderBy } from "firebase/firestore"; const q = query(citiesRef, orderBy("state"), orderBy("population", "desc"));
Web
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];
Kotlin
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);
Java
Python
Python
C++
cities_ref.OrderBy("state").OrderBy("name", Query::Direction::kDescending);
Node.js
Go
PHP
PHP
ดูข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งและสร้างCloud Firestoreไคลเอ็นต์ได้ที่ Cloud Firestoreไลบรารีของไคลเอ็นต์
Unity
Query query = citiesRef.OrderBy("State").OrderByDescending("Population");
C#
Ruby
คุณสามารถรวมตัวกรอง where()
กับ orderBy()
และ limit()
ได้ ใน
ตัวอย่างต่อไปนี้ คำค้นหาจะกำหนดเกณฑ์ประชากร จัดเรียงตามประชากร
จากน้อยไปมาก และแสดงเฉพาะผลลัพธ์ 2-3 รายการแรกที่เกิน
เกณฑ์
Web
import { query, where, orderBy, limit } from "firebase/firestore"; const q = query(citiesRef, where("population", ">", 100000), orderBy("population"), limit(2));
Web
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];
Kotlin
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);
Java
Python
Python
C++
cities_ref.WhereGreaterThan("population", FieldValue::Integer(100000)) .OrderBy("population") .Limit(2);
Node.js
Go
PHP
PHP
ดูข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งและสร้างCloud Firestoreไคลเอ็นต์ได้ที่ Cloud Firestoreไลบรารีของไคลเอ็นต์
Unity
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Population") .Limit(2);
C#
Ruby
อย่างไรก็ตาม หากคุณมีตัวกรองที่มีการเปรียบเทียบช่วง (<
, <=
, >
, >=
)
การจัดลำดับแรกต้องอยู่ในช่องเดียวกัน โปรดดูรายการข้อจำกัดของ orderBy()
ด้านล่าง
ข้อจำกัด
โปรดทราบข้อจำกัดต่อไปนี้สำหรับorderBy()
orderBy()
คลอสยังกรองการมีอยู่ของฟิลด์ที่ระบุด้วย ชุดผลลัพธ์จะไม่รวมเอกสารที่ไม่มีฟิลด์ที่ระบุ
orderBy
และการมีอยู่
เมื่อคุณจัดเรียงการค้นหาตามฟิลด์ที่ระบุ การค้นหาจะแสดงได้เฉพาะ เอกสารที่มีฟิลด์จัดเรียง
ตัวอย่างเช่น การค้นหาต่อไปนี้จะไม่แสดงเอกสารใดๆ ที่ไม่ได้ตั้งค่าฟิลด์
population
แม้ว่าเอกสารนั้นจะตรงกับตัวกรองการค้นหาอื่นๆ ก็ตาม
Java
db.collection("cities").whereEqualTo("country", “USA”).orderBy(“population”);
ผลที่เกี่ยวข้องจะใช้กับอสมการ การค้นหาที่มีตัวกรองความไม่เท่ากัน
ในช่องยังหมายถึงการจัดเรียงตามช่องนั้นด้วย การค้นหาต่อไปนี้จะไม่แสดงเอกสารที่ไม่มีฟิลด์ population
แม้ว่าจะมี country = USA
ในเอกสารนั้นก็ตาม
วิธีแก้ปัญหาชั่วคราวคือคุณสามารถเรียกใช้
การค้นหาแยกกันสำหรับการเรียงลำดับแต่ละรายการ หรือกำหนดค่าสำหรับฟิลด์ทั้งหมด
ที่คุณเรียงลำดับได้
Java
db.collection(“cities”).where(or(“country”, USA”), greaterThan(“population”, 250000));
การค้นหาข้างต้นมีการเรียงลำดับโดยนัยในความไม่เท่ากันและเทียบเท่ากับคำสั่งต่อไปนี้
Java
db.collection(“cities”).where(or(“country”, USA”), greaterThan(“population”, 250000)).orderBy(“population”);