ด้วยเคอร์เซอร์การค้นหาใน Cloud Firestore คุณสามารถแบ่งข้อมูลที่ส่งคืนโดยแบบสอบถามเป็นชุดตามพารามิเตอร์ที่คุณกำหนดในแบบสอบถามของคุณ
เคอร์เซอร์ของข้อความค้นหากำหนดจุดเริ่มต้นและจุดสิ้นสุดของข้อความค้นหา ทำให้คุณสามารถ:
- ส่งกลับส่วนย่อยของข้อมูล
- ผลการค้นหาเลขหน้า
อย่างไรก็ตาม หากต้องการกำหนดช่วงเฉพาะสำหรับข้อความค้นหา คุณควรใช้เมธอด where()
ที่อธิบายไว้ใน Simple Queries
เพิ่มเคอร์เซอร์อย่างง่ายในแบบสอบถาม
ใช้เมธอด startAt()
หรือ startAfter()
เพื่อกำหนดจุดเริ่มต้นสำหรับแบบสอบถาม เมธอด startAt()
รวมจุดเริ่มต้น ในขณะที่เมธอด startAfter()
ไม่รวมจุดนั้น
ตัวอย่างเช่น หากคุณใช้ startAt(A)
ในแบบสอบถาม ก็จะส่งกลับทั้งตัวอักษร หากคุณใช้ startAfter(A)
แทน ระบบจะส่งกลับ BZ
Web modular API
import { query, orderBy, startAt } from "firebase/firestore"; const q = query(citiesRef, orderBy("population"), startAt(1000000));
Web namespaced API
citiesRef.orderBy("population").startAt(1000000);
สวิฟต์
// Get all cities with population over one million, ordered by population. db.collection("cities") .order(by: "population") .start(at: [1000000])
วัตถุประสงค์-C
// Get all cities with population over one million, ordered by population. [[[db collectionWithPath:@"cities"] queryOrderedByField:@"population"] queryStartingAtValues:@[ @1000000 ]];
Kotlin+KTX
// Get all cities with a population >= 1,000,000, ordered by population, db.collection("cities") .orderBy("population") .startAt(1000000)
Java
// Get all cities with a population >= 1,000,000, ordered by population, db.collection("cities") .orderBy("population") .startAt(1000000);
Dart
db.collection("cities").orderBy("population").startAt([1000000]);
ชวา
หลาม
Python
ภาษาซี++
// Get all cities with a population >= 1,000,000, ordered by population, db->Collection("cities") .OrderBy("population") .StartAt({FieldValue::Integer(1000000)});
โหนด js
ไป
พี.เอช.พี
พี.เอช.พี
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งและการสร้างไคลเอนต์ Cloud Firestore โปรดดูที่ Cloud Firestore Client Libraries
ความสามัคคี
Query query = citiesRef.OrderBy("Population").StartAt(1000000);
ค#
ทับทิม
ในทำนองเดียวกัน ใช้เมธอด endAt()
หรือ endBefore()
เพื่อกำหนดจุดสิ้นสุดสำหรับผลลัพธ์การสืบค้นของคุณ
Web modular API
import { query, orderBy, endAt } from "firebase/firestore"; const q = query(citiesRef, orderBy("population"), endAt(1000000));
Web namespaced API
citiesRef.orderBy("population").endAt(1000000);
สวิฟต์
// Get all cities with population less than one million, ordered by population. db.collection("cities") .order(by: "population") .end(at: [1000000])
วัตถุประสงค์-C
// Get all cities with population less than one million, ordered by population. [[[db collectionWithPath:@"cities"] queryOrderedByField:@"population"] queryEndingAtValues:@[ @1000000 ]];
Kotlin+KTX
// Get all cities with a population <= 1,000,000, ordered by population, db.collection("cities") .orderBy("population") .endAt(1000000)
Java
// Get all cities with a population <= 1,000,000, ordered by population, db.collection("cities") .orderBy("population") .endAt(1000000);
Dart
db.collection("cities").orderBy("population").endAt([1000000]);
ชวา
หลาม
Python
ภาษาซี++
// Get all cities with a population <= 1,000,000, ordered by population, db->Collection("cities") .OrderBy("population") .EndAt({FieldValue::Integer(1000000)});
โหนด js
ไป
พี.เอช.พี
พี.เอช.พี
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งและการสร้างไคลเอนต์ Cloud Firestore โปรดดูที่ Cloud Firestore Client Libraries
ความสามัคคี
Query query = citiesRef.OrderBy("Population").EndAt(1000000);
ค#
ทับทิม
ใช้ภาพรวมของเอกสารเพื่อกำหนดเคอร์เซอร์แบบสอบถาม
คุณยังสามารถส่งภาพรวมของเอกสารไปยังส่วนคำสั่งเคอร์เซอร์เป็นจุดเริ่มต้นหรือจุดสิ้นสุดของเคอร์เซอร์แบบสอบถาม ค่าในภาพรวมของเอกสารทำหน้าที่เป็นค่าในเคอร์เซอร์แบบสอบถาม
ตัวอย่างเช่น ถ่ายภาพเอกสาร "ซานฟรานซิสโก" ในชุดข้อมูลของเมืองและประชากรของคุณ จากนั้น ใช้ภาพรวมของเอกสารนั้นเป็นจุดเริ่มต้นสำหรับเคอร์เซอร์แบบสอบถามประชากรของคุณ ข้อความค้นหาของคุณจะแสดงเมืองทั้งหมดที่มีประชากรมากกว่าหรือเท่ากับเมืองซานฟรานซิสโก ตามที่กำหนดไว้ในภาพรวมของเอกสาร
Web modular API
import { collection, doc, getDoc, query, orderBy, startAt } from "firebase/firestore"; const citiesRef = collection(db, "cities"); const docSnap = await getDoc(doc(citiesRef, "SF")); // Get all cities with a population bigger than San Francisco const biggerThanSf = query(citiesRef, orderBy("population"), startAt(docSnap)); // ...
Web namespaced API
var citiesRef = db.collection("cities"); return citiesRef.doc("SF").get().then((doc) => { // Get all cities with a population bigger than San Francisco var biggerThanSf = citiesRef .orderBy("population") .startAt(doc); // ... });
สวิฟต์
db.collection("cities") .document("SF") .addSnapshotListener { (document, error) in guard let document = document else { print("Error retreving cities: \(error.debugDescription)") return } // Get all cities with a population greater than or equal to San Francisco. let sfSizeOrBigger = db.collection("cities") .order(by: "population") .start(atDocument: document) }
วัตถุประสงค์-C
[[[db collectionWithPath:@"cities"] documentWithPath:@"SF"] addSnapshotListener:^(FIRDocumentSnapshot *snapshot, NSError *error) { if (snapshot == nil) { NSLog(@"Error retreiving cities: %@", error); return; } // Get all cities with a population greater than or equal to San Francisco. FIRQuery *sfSizeOrBigger = [[[db collectionWithPath:@"cities"] queryOrderedByField:@"population"] queryStartingAtDocument:snapshot]; }];
Kotlin+KTX
// Get the data for "San Francisco" db.collection("cities").document("SF") .get() .addOnSuccessListener { documentSnapshot -> // Get all cities with a population bigger than San Francisco. val biggerThanSf = db.collection("cities") .orderBy("population") .startAt(documentSnapshot) // ... }
Java
// Get the data for "San Francisco" db.collection("cities").document("SF") .get() .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() { @Override public void onSuccess(DocumentSnapshot documentSnapshot) { // Get all cities with a population bigger than San Francisco. Query biggerThanSf = db.collection("cities") .orderBy("population") .startAt(documentSnapshot); // ... } });
Dart
db.collection("cities").doc("SF").get().then( (documentSnapshot) { final biggerThanSf = db .collection("cities") .orderBy("population") .startAt([documentSnapshot]); }, onError: (e) => print("Error: $e"), );
ชวา
หลาม
Python
ภาษาซี++
db->Collection("cities").Document("SF").Get().OnCompletion( [db](const Future<DocumentSnapshot>& future) { if (future.error() == Error::kErrorOk) { const DocumentSnapshot& document_snapshot = *future.result(); Query bigger_than_sf = db->Collection("cities") .OrderBy("population") .StartAt({document_snapshot}); // ... } });
โหนด js
ไป
พี.เอช.พี
พี.เอช.พี
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งและการสร้างไคลเอนต์ Cloud Firestore โปรดดูที่ Cloud Firestore Client Libraries
ความสามัคคี
CollectionReference citiesRef = db.Collection("cities"); DocumentReference docRef = citiesRef.Document("SF"); docRef.GetSnapshotAsync().ContinueWith((snapshotTask) => { Query query = citiesRef.OrderBy("Population").StartAt(snapshotTask.Result); });
ค#
ทับทิม
ใส่เลขหน้าแบบสอบถาม
แบ่งคิวรีด้วยการรวมเคอร์เซอร์คิวรีเข้ากับเมธอด limit()
ตัวอย่างเช่น ใช้เอกสารชุดสุดท้ายเป็นจุดเริ่มต้นของเคอร์เซอร์สำหรับชุดถัดไป
Web modular API
import { collection, query, orderBy, startAfter, limit, getDocs } from "firebase/firestore"; // Query the first page of docs const first = query(collection(db, "cities"), orderBy("population"), limit(25)); const documentSnapshots = await getDocs(first); // Get the last visible document const lastVisible = documentSnapshots.docs[documentSnapshots.docs.length-1]; console.log("last", lastVisible); // Construct a new query starting at this document, // get the next 25 cities. const next = query(collection(db, "cities"), orderBy("population"), startAfter(lastVisible), limit(25));
Web namespaced API
var first = db.collection("cities") .orderBy("population") .limit(25); return first.get().then((documentSnapshots) => { // Get the last visible document var lastVisible = documentSnapshots.docs[documentSnapshots.docs.length-1]; console.log("last", lastVisible); // Construct a new query starting at this document, // get the next 25 cities. var next = db.collection("cities") .orderBy("population") .startAfter(lastVisible) .limit(25); });
สวิฟต์
// Construct query for first 25 cities, ordered by population let first = db.collection("cities") .order(by: "population") .limit(to: 25) first.addSnapshotListener { (snapshot, error) in guard let snapshot = snapshot else { print("Error retreving cities: \(error.debugDescription)") return } guard let lastSnapshot = snapshot.documents.last else { // The collection is empty. return } // Construct a new query starting after this document, // retrieving the next 25 cities. let next = db.collection("cities") .order(by: "population") .start(afterDocument: lastSnapshot) // Use the query for pagination. // ... }
วัตถุประสงค์-C
FIRQuery *first = [[[db collectionWithPath:@"cities"] queryOrderedByField:@"population"] queryLimitedTo:25]; [first addSnapshotListener:^(FIRQuerySnapshot *snapshot, NSError *error) { if (snapshot == nil) { NSLog(@"Error retreiving cities: %@", error); return; } if (snapshot.documents.count == 0) { return; } FIRDocumentSnapshot *lastSnapshot = snapshot.documents.lastObject; // Construct a new query starting after this document, // retreiving the next 25 cities. FIRQuery *next = [[[db collectionWithPath:@"cities"] queryOrderedByField:@"population"] queryStartingAfterDocument:lastSnapshot]; // Use the query for pagination. // ... }];
Kotlin+KTX
// Construct query for first 25 cities, ordered by population val first = db.collection("cities") .orderBy("population") .limit(25) first.get() .addOnSuccessListener { documentSnapshots -> // ... // Get the last visible document val lastVisible = documentSnapshots.documents[documentSnapshots.size() - 1] // Construct a new query starting at this document, // get the next 25 cities. val next = db.collection("cities") .orderBy("population") .startAfter(lastVisible) .limit(25) // Use the query for pagination // ... }
Java
// Construct query for first 25 cities, ordered by population Query first = db.collection("cities") .orderBy("population") .limit(25); first.get() .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() { @Override public void onSuccess(QuerySnapshot documentSnapshots) { // ... // Get the last visible document DocumentSnapshot lastVisible = documentSnapshots.getDocuments() .get(documentSnapshots.size() -1); // Construct a new query starting at this document, // get the next 25 cities. Query next = db.collection("cities") .orderBy("population") .startAfter(lastVisible) .limit(25); // Use the query for pagination // ... } });
Dart
// Construct query for first 25 cities, ordered by population final first = db.collection("cities").orderBy("population").limit(25); first.get().then( (documentSnapshots) { // Get the last visible document final lastVisible = documentSnapshots.docs[documentSnapshots.size - 1]; // Construct a new query starting at this document, // get the next 25 cities. final next = db .collection("cities") .orderBy("population") .startAfter([lastVisible]).limit(25); // Use the query for pagination // ... }, onError: (e) => print("Error completing: $e"), );
ชวา
หลาม
Python
ภาษาซี++
// Construct query for first 25 cities, ordered by population Query first = db->Collection("cities").OrderBy("population").Limit(25); first.Get().OnCompletion([db](const Future<QuerySnapshot>& future) { if (future.error() != Error::kErrorOk) { // Handle error... return; } // Get the last visible document const QuerySnapshot& document_snapshots = *future.result(); std::vector<DocumentSnapshot> documents = document_snapshots.documents(); const DocumentSnapshot& last_visible = documents.back(); // Construct a new query starting at this document, // get the next 25 cities. Query next = db->Collection("cities") .OrderBy("population") .StartAfter(last_visible) .Limit(25); // Use the query for pagination // ... });
โหนด js
ไป
พี.เอช.พี
พี.เอช.พี
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งและการสร้างไคลเอนต์ Cloud Firestore โปรดดูที่ Cloud Firestore Client Libraries
ความสามัคคี
CollectionReference citiesRef = db.Collection("cities"); Query firstQuery = citiesRef.OrderBy("Population").Limit(3); // Get the last document from the results firstQuery.GetSnapshotAsync().ContinueWith((querySnapshotTask) => { long lastPopulation = 0; foreach (DocumentSnapshot documentSnapshot in querySnapshotTask.Result.Documents) { lastPopulation = documentSnapshot.GetValue<long>("Population"); } // Construct a new query starting at this document. // Note: this will not have the desired effect if multiple cities have the exact same population value Query secondQuery = citiesRef.OrderBy("Population").StartAfter(lastPopulation); Task<QuerySnapshot> secondQuerySnapshot = secondQuery.GetSnapshotAsync();
ค#
ทับทิม
ตั้งเคอร์เซอร์ตามหลายฟิลด์
เมื่อใช้เคอร์เซอร์ตามค่าฟิลด์ (ไม่ใช่ DocumentSnapshot) คุณสามารถทำให้ตำแหน่งเคอร์เซอร์แม่นยำยิ่งขึ้นได้โดยการเพิ่มฟิลด์เพิ่มเติม ซึ่งจะเป็นประโยชน์อย่างยิ่งหากชุดข้อมูลของคุณมีเอกสารหลายชุดซึ่งทั้งหมดมีค่าเท่ากันสำหรับช่องเคอร์เซอร์ของคุณ ทำให้ตำแหน่งของเคอร์เซอร์คลุมเครือ คุณสามารถเพิ่มค่าฟิลด์เพิ่มเติมในเคอร์เซอร์ของคุณเพื่อระบุจุดเริ่มต้นหรือจุดสิ้นสุดเพิ่มเติม และลดความคลุมเครือ
ตัวอย่างเช่น ในชุดข้อมูลที่มีเมืองทั้งหมดชื่อ "สปริงฟิลด์" ในสหรัฐอเมริกา จะมีจุดเริ่มต้นหลายจุดสำหรับชุดข้อความค้นหาที่จะเริ่มต้นที่ "สปริงฟิลด์":
เมือง | |
---|---|
ชื่อ | สถานะ |
สปริงฟิลด์ | แมสซาชูเซตส์ |
สปริงฟิลด์ | รัฐมิสซูรี |
สปริงฟิลด์ | วิสคอนซิน |
หากต้องการเริ่มต้นที่ Springfield เฉพาะ คุณสามารถเพิ่มสถานะเป็นเงื่อนไขรองในส่วนคำสั่งเคอร์เซอร์ของคุณ
Web modular API
// Will return all Springfields import { collection, query, orderBy, startAt } from "firebase/firestore"; const q1 = query(collection(db, "cities"), orderBy("name"), orderBy("state"), startAt("Springfield")); // Will return "Springfield, Missouri" and "Springfield, Wisconsin" const q2 = query(collection(db, "cities"), orderBy("name"), orderBy("state"), startAt("Springfield", "Missouri"));
Web namespaced API
// Will return all Springfields db.collection("cities") .orderBy("name") .orderBy("state") .startAt("Springfield"); // Will return "Springfield, Missouri" and "Springfield, Wisconsin" db.collection("cities") .orderBy("name") .orderBy("state") .startAt("Springfield", "Missouri");
สวิฟต์
// Will return all Springfields db.collection("cities") .order(by: "name") .order(by: "state") .start(at: ["Springfield"]) // Will return "Springfield, Missouri" and "Springfield, Wisconsin" db.collection("cities") .order(by: "name") .order(by: "state") .start(at: ["Springfield", "Missouri"])
วัตถุประสงค์-C
// Will return all Springfields [[[[db collectionWithPath:@"cities"] queryOrderedByField:@"name"] queryOrderedByField:@"state"] queryStartingAtValues:@[ @"Springfield" ]]; // Will return "Springfield, Missouri" and "Springfield, Wisconsin" [[[[db collectionWithPath:@"cities"] queryOrderedByField:@"name"] queryOrderedByField:@"state"] queryStartingAtValues:@[ @"Springfield", @"Missouri" ]];
Kotlin+KTX
// Will return all Springfields db.collection("cities") .orderBy("name") .orderBy("state") .startAt("Springfield") // Will return "Springfield, Missouri" and "Springfield, Wisconsin" db.collection("cities") .orderBy("name") .orderBy("state") .startAt("Springfield", "Missouri")
Java
// Will return all Springfields db.collection("cities") .orderBy("name") .orderBy("state") .startAt("Springfield"); // Will return "Springfield, Missouri" and "Springfield, Wisconsin" db.collection("cities") .orderBy("name") .orderBy("state") .startAt("Springfield", "Missouri");
Dart
// Will return all Springfields db .collection("cities") .orderBy("name") .orderBy("state") .startAt(["Springfield"]); // Will return "Springfield, Missouri" and "Springfield, Wisconsin" db .collection("cities") .orderBy("name") .orderBy("state") .startAt(["Springfield", "Missouri"]);
ชวา
หลาม
Python
ภาษาซี++
// This is not yet supported.
โหนด js
ไป
พี.เอช.พี
พี.เอช.พี
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งและการสร้างไคลเอนต์ Cloud Firestore โปรดดูที่ Cloud Firestore Client Libraries
ความสามัคคี
Query query1 = db.Collection("cities").OrderBy("Name").OrderBy("State").StartAt("Springfield"); Query query2 = db.Collection("cities").OrderBy("Name").OrderBy("State").StartAt("Springfield", "Missouri");