Cloud Firestore มีฟังก์ชันการสืบค้นที่มีประสิทธิภาพสำหรับการระบุเอกสารที่คุณต้องการดึงจากคอลเลกชัน ข้อความค้นหาเหล่านี้ยังสามารถใช้กับ get()
หรือ addSnapshotListener()
ตามที่อธิบายไว้ใน Get Data
สั่งซื้อและจำกัดข้อมูล
ตามค่าเริ่มต้น คิวรีจะดึงเอกสารทั้งหมดที่ตรงกับคิวรีในลำดับจากน้อยไปมากตาม ID เอกสาร คุณสามารถระบุลำดับการจัดเรียงสำหรับข้อมูลของคุณโดยใช้ orderBy()
และคุณสามารถจำกัดจำนวนเอกสารที่ดึงมาโดยใช้ limit()
หากคุณระบุ limit()
ค่าจะต้องมากกว่าหรือเท่ากับศูนย์
ตัวอย่างเช่น คุณสามารถค้นหา 3 เมืองแรกตามตัวอักษรด้วย:
Web modular API
import { query, orderBy, limit } from "firebase/firestore"; const q = query(citiesRef, orderBy("name"), limit(3));
Web namespaced API
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
ภาษาซี++
cities_ref.OrderBy("name").Limit(3);
โหนด js
ไป
พี.เอช.พี
พี.เอช.พี
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งและการสร้างไคลเอ็นต์ Cloud Firestore โปรดดูที่ Cloud Firestore Client Libraries
ความสามัคคี
Query query = citiesRef.OrderBy("Name").Limit(3);
ค#
ทับทิม
คุณยังสามารถเรียงลำดับจากมากไปน้อยเพื่อให้ได้ 3 เมือง ล่าสุด :
Web modular API
import { query, orderBy, limit } from "firebase/firestore"; const q = query(citiesRef, orderBy("name", "desc"), limit(3));
Web namespaced API
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
ภาษาซี++
cities_ref.OrderBy("name", Query::Direction::kDescending).Limit(3);
โหนด js
ไป
พี.เอช.พี
พี.เอช.พี
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งและการสร้างไคลเอ็นต์ Cloud Firestore โปรดดูที่ Cloud Firestore Client Libraries
ความสามัคคี
Query query = citiesRef.OrderByDescending("Name").Limit(3);
ค#
ทับทิม
คุณยังสามารถสั่งซื้อได้หลายช่อง ตัวอย่างเช่น หากคุณต้องการเรียงลำดับตามรัฐ และภายในแต่ละรัฐจะเรียงลำดับตามจำนวนประชากรจากมากไปหาน้อย:
Web modular API
import { query, orderBy } from "firebase/firestore"; const q = query(citiesRef, orderBy("state"), orderBy("population", "desc"));
Web namespaced API
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
ภาษาซี++
cities_ref.OrderBy("state").OrderBy("name", Query::Direction::kDescending);
โหนด js
ไป
พี.เอช.พี
พี.เอช.พี
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งและการสร้างไคลเอ็นต์ Cloud Firestore โปรดดูที่ Cloud Firestore Client Libraries
ความสามัคคี
Query query = citiesRef.OrderBy("State").OrderByDescending("Population");
ค#
ทับทิม
คุณสามารถรวมตัวกรอง where()
กับ orderBy()
และ limit()
ในตัวอย่างต่อไปนี้ เคียวรีกำหนดเกณฑ์ของประชากร จัดเรียงตามจำนวนประชากรจากน้อยไปมาก และส่งคืนเฉพาะผลลัพธ์สองสามรายการแรกที่เกินเกณฑ์:
Web modular API
import { query, where, orderBy, limit } from "firebase/firestore"; const q = query(citiesRef, where("population", ">", 100000), orderBy("population"), limit(2));
Web namespaced API
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
ภาษาซี++
cities_ref.WhereGreaterThan("population", FieldValue::Integer(100000)) .OrderBy("population") .Limit(2);
โหนด js
ไป
พี.เอช.พี
พี.เอช.พี
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งและการสร้างไคลเอ็นต์ Cloud Firestore โปรดดูที่ Cloud Firestore Client Libraries
ความสามัคคี
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Population") .Limit(2);
ค#
ทับทิม
อย่างไรก็ตาม หากคุณมีตัวกรองที่มีการเปรียบเทียบช่วง ( <
, <=
, >
, >=
) ลำดับแรกของคุณจะต้องอยู่ในฟิลด์เดียวกัน โปรดดูรายการข้อจำกัดของ orderBy()
ด้านล่าง
ข้อจำกัด
สังเกตข้อจำกัดต่อไปนี้สำหรับคำ orderBy()
:
- ส่วนคำสั่ง
orderBy()
ยัง กรองการมีอยู่ของฟิลด์ที่กำหนด ชุดผลลัพธ์จะไม่รวมเอกสารที่ไม่มีฟิลด์ที่กำหนด หากคุณรวมตัวกรองที่มีการเปรียบเทียบช่วง (
<
,<=
,>
,>=
) ลำดับแรกของคุณต้องอยู่ในฟิลด์เดียวกัน:ถูกต้อง : ตัวกรองช่วงและ
orderBy
ในฟิลด์เดียวกันWeb modular API
import { query, where, orderBy } from "firebase/firestore"; const q = query(citiesRef, where("population", ">", 100000), orderBy("population"));
Web namespaced API
citiesRef.where("population", ">", 100000).orderBy("population");
สวิฟต์
หมายเหตุ: ผลิตภัณฑ์นี้ไม่พร้อมใช้งานบนเป้าหมาย watchOS และ App ClipcitiesRef .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
โหนด js
ไป
พี.เอช.พี
พี.เอช.พี
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งและการสร้างไคลเอ็นต์ Cloud Firestore โปรดดูที่ Cloud Firestore Client Libraries
ความสามัคคี
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Population");
ค#
ทับทิม
ไม่ถูกต้อง : ตัวกรองช่วงและ
orderBy
แรกโดยในฟิลด์อื่นWeb modular API
import { query, where, orderBy } from "firebase/firestore"; const q = query(citiesRef, where("population", ">", 100000), orderBy("country"));
Web namespaced API
citiesRef.where("population", ">", 100000).orderBy("country");
สวิฟต์
หมายเหตุ: ผลิตภัณฑ์นี้ไม่พร้อมใช้งานบนเป้าหมาย watchOS และ App ClipcitiesRef .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
ภาษาซี++
// BAD EXAMPLE -- will crash the program: cities_ref.WhereGreaterThan("population", FieldValue::Integer(100000)) .OrderBy("country");
โหนด js
ไป
พี.เอช.พี
พี.เอช.พี
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งและการสร้างไคลเอ็นต์ Cloud Firestore โปรดดูที่ Cloud Firestore Client Libraries
ความสามัคคี
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Country");
ค#
ทับทิม
- คุณไม่สามารถเรียงลำดับข้อความค้นหาของคุณตามฟิลด์ใดๆ ที่รวมอยู่ในความเท่าเทียมกัน (
=
) หรือin
อนุประโยค
orderBy
และการมีอยู่
เมื่อคุณจัดลำดับคิวรีตามฟิลด์ที่กำหนด คิวรีสามารถส่งกลับเฉพาะเอกสารที่มีฟิลด์ลำดับตามอยู่เท่านั้น
ตัวอย่างเช่น ข้อความค้นหาต่อไปนี้จะไม่ส่งคืนเอกสารใดๆ ที่ไม่ได้ตั้งค่าฟิลด์ population
แม้ว่าจะเป็นไปตามตัวกรองข้อความค้นหาก็ตาม
ชวา
db.collection("cities").whereEqualTo("country", “USA”).orderBy(“population”);
ผลที่เกี่ยวข้องใช้กับความไม่เท่าเทียมกัน ข้อความค้นหาที่มีตัวกรองความไม่เท่าเทียมกันในฟิลด์ยังแสดงถึงการจัดลำดับตามฟิลด์นั้นด้วย ข้อความค้นหาต่อไปนี้ไม่ส่งคืนเอกสารที่ไม่มีฟิลด์ population
แม้ว่า country = USA
ในเอกสารนั้น เพื่อแก้ปัญหาชั่วคราว คุณสามารถดำเนินการแบบสอบถามแยกต่างหากสำหรับแต่ละการสั่งซื้อ หรือคุณสามารถกำหนดค่าสำหรับฟิลด์ทั้งหมดที่คุณสั่งซื้อ
ชวา
db.collection(“cities”).where(or(“country”, USA”), greaterThan(“population”, 250000));
ข้อความค้นหาด้านบนมีลำดับโดยนัยเกี่ยวกับอสมการและเทียบเท่ากับสิ่งต่อไปนี้:
ชวา
db.collection(“cities”).where(or(“country”, USA”), greaterThan(“population”, 250000)).orderBy(“population”);