Cloud Firestore কোয়েরি কার্সারের সাহায্যে, আপনি আপনার কোয়েরিতে সংজ্ঞায়িত পরামিতি অনুসারে একটি কোয়েরি দ্বারা ফেরত আসা ডেটাকে ব্যাচে বিভক্ত করতে পারেন।
কোয়েরি কার্সারগুলি একটি কোয়েরির শুরু এবং শেষ বিন্দু নির্ধারণ করে, যা আপনাকে নিম্নলিখিতগুলি করতে দেয়:
- ডেটার একটি উপসেট ফেরত দিন।
- পৃষ্ঠাবদ্ধ কোয়েরির ফলাফল।
তবে, একটি কোয়েরির জন্য একটি নির্দিষ্ট পরিসর নির্ধারণ করতে, আপনার Simple Queries এ বর্ণিত where() পদ্ধতিটি ব্যবহার করা উচিত।
একটি কোয়েরিতে একটি সাধারণ কার্সার যোগ করুন
একটি কোয়েরির স্টার্ট পয়েন্ট নির্ধারণ করতে startAt() অথবা startAfter() পদ্ধতি ব্যবহার করুন। startAt() পদ্ধতিতে স্টার্ট পয়েন্ট অন্তর্ভুক্ত থাকে, যখন startAfter() পদ্ধতিতে এটি বাদ দেওয়া হয়।
উদাহরণস্বরূপ, যদি আপনি একটি কোয়েরিতে startAt(A) ব্যবহার করেন, তাহলে এটি সম্পূর্ণ বর্ণমালা ফেরত দেয়। যদি আপনি এর পরিবর্তে startAfter(A) ব্যবহার করেন, তাহলে এটি BZ ফেরত দেয়।
Web
import { query, orderBy, startAt } from "firebase/firestore"; const q = query(citiesRef, orderBy("population"), startAt(1000000));
Web
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 ]];
Kotlin
// 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)});
নোড.জেএস
যাও
পিএইচপি
পিএইচপি
Cloud Firestore ক্লায়েন্ট ইনস্টল এবং তৈরি করার বিষয়ে আরও জানতে, Cloud Firestore ক্লায়েন্ট লাইব্রেরি দেখুন।
ঐক্য
Query query = citiesRef.OrderBy("Population").StartAt(1000000);
সি#
রুবি
একইভাবে, আপনার কোয়েরির ফলাফলের জন্য একটি শেষ বিন্দু নির্ধারণ করতে endAt() অথবা endBefore() পদ্ধতি ব্যবহার করুন।
Web
import { query, orderBy, endAt } from "firebase/firestore"; const q = query(citiesRef, orderBy("population"), endAt(1000000));
Web
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 ]];
Kotlin
// 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)});
নোড.জেএস
যাও
পিএইচপি
পিএইচপি
Cloud Firestore ক্লায়েন্ট ইনস্টল এবং তৈরি করার বিষয়ে আরও জানতে, Cloud Firestore ক্লায়েন্ট লাইব্রেরি দেখুন।
ঐক্য
Query query = citiesRef.OrderBy("Population").EndAt(1000000);
সি#
রুবি
কোয়েরি কার্সার সংজ্ঞায়িত করতে একটি ডকুমেন্ট স্ন্যাপশট ব্যবহার করুন
আপনি কোয়েরি কার্সারের শুরু বা শেষ বিন্দু হিসেবে কার্সার ক্লজে একটি ডকুমেন্ট স্ন্যাপশটও পাস করতে পারেন। ডকুমেন্ট স্ন্যাপশটের মানগুলি কোয়েরি কার্সারের মান হিসেবে কাজ করে।
উদাহরণস্বরূপ, আপনার শহর এবং জনসংখ্যার ডেটা সেটে "সান ফ্রান্সিসকো" ডকুমেন্টের একটি স্ন্যাপশট নিন। তারপর, আপনার জনসংখ্যা কোয়েরি কার্সারের জন্য সেই ডকুমেন্ট স্ন্যাপশটটি শুরুর বিন্দু হিসাবে ব্যবহার করুন। আপনার কোয়েরি ডকুমেন্ট স্ন্যাপশটে সংজ্ঞায়িত অনুসারে, সান ফ্রান্সিসকোর জনসংখ্যার চেয়ে বড় বা সমান সমস্ত শহর ফিরিয়ে দেবে।
Web
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
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]; }];
Kotlin
// 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") .startAtDocument(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}); // ... } });
নোড.জেএস
যাও
পিএইচপি
পিএইচপি
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() পদ্ধতির সাথে কোয়েরি কার্সার একত্রিত করে কোয়েরিগুলিকে পৃষ্ঠাবদ্ধ করুন। উদাহরণস্বরূপ, পরবর্তী ব্যাচের জন্য কার্সারের শুরু হিসাবে একটি ব্যাচের শেষ ডকুমেন্টটি ব্যবহার করুন।
Web
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
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. // ... }];
Kotlin
// 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") .startAfterDocument(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 // ... });
নোড.জেএস
যাও
পিএইচপি
পিএইচপি
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();
সি#
রুবি
একাধিক ক্ষেত্রের উপর ভিত্তি করে কার্সার সেট করুন
যখন আপনি একটি ফিল্ড মানের (ডকুমেন্টস্ন্যাপশট নয়) উপর ভিত্তি করে কার্সার ব্যবহার করেন, তখন আপনি অতিরিক্ত ক্ষেত্র যোগ করে কার্সারের অবস্থান আরও সুনির্দিষ্ট করতে পারেন। এটি বিশেষভাবে কার্যকর যদি আপনার ডেটা সেটে একাধিক ডকুমেন্ট থাকে যার সকলেরই আপনার কার্সার ক্ষেত্রের জন্য একই মান থাকে, যা কার্সারের অবস্থানকে অস্পষ্ট করে তোলে। আপনি শুরু বা শেষ বিন্দু আরও নির্দিষ্ট করতে এবং অস্পষ্টতা কমাতে আপনার কার্সারে অতিরিক্ত ফিল্ড মান যোগ করতে পারেন।
উদাহরণস্বরূপ, মার্কিন যুক্তরাষ্ট্রের "স্প্রিংফিল্ড" নামক সমস্ত শহর সম্বলিত একটি ডেটা সেটে, "স্প্রিংফিল্ড" থেকে শুরু হওয়া একটি কোয়েরি সেটের জন্য একাধিক সূচনা বিন্দু থাকবে:
| শহর | |
|---|---|
| নাম | রাজ্য |
| স্প্রিংফিল্ড | ম্যাসাচুসেটস |
| স্প্রিংফিল্ড | মিসৌরি |
| স্প্রিংফিল্ড | উইসকনসিন |
একটি নির্দিষ্ট স্প্রিংফিল্ড থেকে শুরু করতে, আপনি আপনার কার্সার ক্লজে একটি গৌণ শর্ত হিসেবে state যোগ করতে পারেন।
Web
// 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
// 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" ]];
Kotlin
// 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.নোড.জেএস
যাও
পিএইচপি
পিএইচপি
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");