Cloud Firestore में क्वेरी कर्सर की मदद से, क्वेरी से मिले डेटा को बैच में बांटा जा सकता है. इसके लिए, क्वेरी में तय किए गए पैरामीटर का इस्तेमाल किया जाता है.
क्वेरी कर्सर, क्वेरी के शुरू और आखिर के पॉइंट तय करते हैं. इससे, आपको ये काम करने की सुविधा मिलती है:
- डेटा का सबसेट दिखाता है.
- क्वेरी के नतीजों को पेज पर दिखाएं.
हालांकि, किसी क्वेरी के लिए कोई खास रेंज तय करने के लिए, आपको where()
सामान्य क्वेरी में बताए गए तरीके का इस्तेमाल करना चाहिए.
क्वेरी में आसान कर्सर जोड़ना
क्वेरी के शुरुआती पॉइंट को तय करने के लिए, startAt()
या startAfter()
का इस्तेमाल करें.
startAt()
तरीके में शुरुआती पॉइंट शामिल होता है, जबकि startAfter()
तरीके में ऐसा नहीं होता.
उदाहरण के लिए, अगर किसी क्वेरी में startAt(A)
का इस्तेमाल किया जाता है, तो यह पूरी वर्णमाला दिखाता है.
इसके बजाय, startAfter(A)
का इस्तेमाल करने पर, B-Z
दिखता है.
import { query, orderBy, startAt } from "firebase/firestore"; const q = query(citiesRef, orderBy("population"), startAt(1000000));
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])
// Get all cities with population over one million, ordered by population. [[[db collectionWithPath:@"cities"] queryOrderedByField:@"population"] queryStartingAtValues:@[ @1000000 ]];
// Get all cities with a population >= 1,000,000, ordered by population, db.collection("cities") .orderBy("population") .startAt(1000000)
// Get all cities with a population >= 1,000,000, ordered by population, db.collection("cities") .orderBy("population") .startAt(1000000);
db.collection("cities").orderBy("population").startAt([1000000]);
// Get all cities with a population >= 1,000,000, ordered by population, db->Collection("cities") .OrderBy("population") .StartAt({FieldValue::Integer(1000000)});
PHP
Cloud Firestore क्लाइंट को इंस्टॉल करने और बनाने के बारे में ज़्यादा जानने के लिए, Cloud Firestore क्लाइंट लाइब्रेरी देखें.
Query query = citiesRef.OrderBy("Population").StartAt(1000000);
इसी तरह, अपनी क्वेरी के नतीजों के लिए एंडपॉइंट तय करने के लिए, endAt()
या endBefore()
तरीकों का इस्तेमाल करें.
import { query, orderBy, endAt } from "firebase/firestore"; const q = query(citiesRef, orderBy("population"), endAt(1000000));
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])
// Get all cities with population less than one million, ordered by population. [[[db collectionWithPath:@"cities"] queryOrderedByField:@"population"] queryEndingAtValues:@[ @1000000 ]];
// Get all cities with a population <= 1,000,000, ordered by population, db.collection("cities") .orderBy("population") .endAt(1000000)
// Get all cities with a population <= 1,000,000, ordered by population, db.collection("cities") .orderBy("population") .endAt(1000000);
db.collection("cities").orderBy("population").endAt([1000000]);
// Get all cities with a population <= 1,000,000, ordered by population, db->Collection("cities") .OrderBy("population") .EndAt({FieldValue::Integer(1000000)});
PHP
Cloud Firestore क्लाइंट को इंस्टॉल करने और बनाने के बारे में ज़्यादा जानने के लिए, Cloud Firestore क्लाइंट लाइब्रेरी देखें.
Query query = citiesRef.OrderBy("Population").EndAt(1000000);
क्वेरी कर्सर तय करने के लिए, दस्तावेज़ के स्नैपशॉट का इस्तेमाल करना
क्वेरी कर्सर के शुरुआती या आखिरी पॉइंट के तौर पर, कर्सर क्लॉज़ में दस्तावेज़ का स्नैपशॉट भी पास किया जा सकता है. दस्तावेज़ के स्नैपशॉट में मौजूद वैल्यू, क्वेरी कर्सर में वैल्यू के तौर पर काम करती हैं.
उदाहरण के लिए, शहरों और जनसंख्या के डेटासेट में "सैन फ़्रांसिस्को" दस्तावेज़ का स्नैपशॉट लें. इसके बाद, उस दस्तावेज़ के स्नैपशॉट का इस्तेमाल, जनसंख्या क्वेरी कर्सर के शुरुआती बिंदु के तौर पर करें. आपकी क्वेरी से, ऐसे सभी शहरों की जानकारी मिलेगी जिनकी जनसंख्या, सैन फ़्रांसिस्को की जनसंख्या के बराबर या उससे ज़्यादा है. इसकी जानकारी, दस्तावेज़ के स्नैपशॉट में दी गई है.
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)); // ...
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) }
[[[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]; }];
// 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) // ... }
// 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); // ... } });
db.collection("cities").doc("SF").get().then( (documentSnapshot) { final biggerThanSf = db .collection("cities") .orderBy("population") .startAtDocument(documentSnapshot); }, onError: (e) => print("Error: $e"), );
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}); // ... } });
PHP
Cloud Firestore क्लाइंट को इंस्टॉल करने और बनाने के बारे में ज़्यादा जानने के लिए, Cloud Firestore क्लाइंट लाइब्रेरी देखें.
CollectionReference citiesRef = db.Collection("cities"); DocumentReference docRef = citiesRef.Document("SF"); docRef.GetSnapshotAsync().ContinueWith((snapshotTask) => { Query query = citiesRef.OrderBy("Population").StartAt(snapshotTask.Result); });
क्वेरी को पेज पर दिखाना
limit()
तरीके के साथ क्वेरी कर्सर को जोड़कर, क्वेरी को पेज पर दिखाएं.
उदाहरण के लिए, अगले बैच के लिए कर्सर की शुरुआत के तौर पर, बैच के आखिरी दस्तावेज़ का इस्तेमाल करें.
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));
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. // ... }
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. // ... }];
// 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 // ... }
// 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 // ... } });
// 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") .startAfterDocument(lastVisible) .limit(25); // Use the query for pagination // ... }, onError: (e) => print("Error completing: $e"), );
// 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 // ... });
PHP
Cloud Firestore क्लाइंट को इंस्टॉल करने और बनाने के बारे में ज़्यादा जानने के लिए, Cloud Firestore क्लाइंट लाइब्रेरी देखें.
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" नाम वाले सभी शहरों के डेटा सेट में, "Springfield" से शुरू होने वाली क्वेरी के लिए कई शुरुआती पॉइंट होंगे:
शहर | |
---|---|
नाम | राज्य |
Springfield | मेसाचुसेट्स |
Springfield | मिसौरी |
Springfield | विस्कॉन्सिन |
किसी खास स्प्रिंगफ़ील्ड से शुरू करने के लिए, कर्सर क्लॉज़ में राज्य को दूसरी शर्त के तौर पर जोड़ा जा सकता है.
// 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"));
// 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"])
// 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" ]];
// 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") .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") .orderBy("name") .orderBy("state") .startAt(["Springfield"]); // Will return "Springfield, Missouri" and "Springfield, Wisconsin" db .collection("cities") .orderBy("name") .orderBy("state") .startAt(["Springfield", "Missouri"]);
// This is not yet supported.
PHP
Cloud Firestore क्लाइंट को इंस्टॉल करने और बनाने के बारे में ज़्यादा जानने के लिए, Cloud Firestore क्लाइंट लाइब्रेरी देखें.
Query query1 = db.Collection("cities").OrderBy("Name").OrderBy("State").StartAt("Springfield"); Query query2 = db.Collection("cities").OrderBy("Name").OrderBy("State").StartAt("Springfield", "Missouri");