Cloud Firestore में क्वेरी की बेहतरीन सुविधा मिलती है. इससे यह तय किया जा सकता है कि आपको किसी कलेक्शन से कौनसे दस्तावेज़ वापस पाने हैं. इन क्वेरी का इस्तेमाल, get() या addSnapshotListener() के साथ भी किया जा सकता है. इसके बारे में डेटा पाएं में बताया गया है.
डेटा को क्रम से लगाना और सीमित करना
डिफ़ॉल्ट रूप से, क्वेरी से वे सभी दस्तावेज़ मिलते हैं जो क्वेरी के मुताबिक होते हैं. ये दस्तावेज़, आईडी के हिसाब से बढ़ते क्रम में होते हैं. orderBy() का इस्तेमाल करके, अपने डेटा को क्रम से लगाने का तरीका तय किया जा सकता है. साथ ही, limit() का इस्तेमाल करके, वापस लाए गए दस्तावेज़ों की संख्या को सीमित किया जा सकता है. अगर आपने limit() की वैल्यू तय की है, तो वह शून्य से ज़्यादा या उसके बराबर होनी चाहिए.
उदाहरण के लिए, वर्णमाला के क्रम में पहले तीन शहरों के लिए क्वेरी की जा सकती है:
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
जाएं
PHP
PHP
Cloud Firestore क्लाइंट को इंस्टॉल करने और बनाने के बारे में ज़्यादा जानने के लिए, Cloud Firestore क्लाइंट लाइब्रेरी देखें.
Unity
Query query = citiesRef.OrderBy("Name").Limit(3);
C#
Ruby
आखिरी तीन शहरों की जानकारी पाने के लिए, घटते क्रम में भी लगाया जा सकता है:
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
जाएं
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
जाएं
PHP
PHP
Cloud Firestore क्लाइंट को इंस्टॉल करने और बनाने के बारे में ज़्यादा जानने के लिए, Cloud Firestore क्लाइंट लाइब्रेरी देखें.
Unity
Query query = citiesRef.OrderBy("State").OrderByDescending("Population");
C#
Ruby
where() फ़िल्टर को orderBy() और limit() के साथ जोड़ा जा सकता है. यहां दिए गए उदाहरण में, क्वेरी से जनसंख्या की थ्रेशोल्ड तय की जाती है. साथ ही, जनसंख्या के हिसाब से बढ़ते क्रम में डेटा को क्रम से लगाया जाता है. इसके बाद, थ्रेशोल्ड से ज़्यादा वाले सिर्फ़ कुछ शुरुआती नतीजे दिखाए जाते हैं:
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
जाएं
PHP
PHP
Cloud Firestore क्लाइंट को इंस्टॉल करने और बनाने के बारे में ज़्यादा जानने के लिए, Cloud Firestore क्लाइंट लाइब्रेरी देखें.
Unity
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Population") .Limit(2);
C#
Ruby
हालांकि, अगर आपके पास रेंज की तुलना (<, <=, >, >=) वाला फ़िल्टर है, तो आपका पहला ऑर्डर उसी फ़ील्ड पर होना चाहिए. नीचे दी गई orderBy() सीमाओं की सूची देखें.
सीमाएं
orderBy() क्लॉज़ के लिए, इस पाबंदी का ध्यान रखें:
orderBy()क्लॉज़, दिए गए फ़ील्ड की मौजूदगी के लिए भी फ़िल्टर करता है. नतीजों के सेट में ऐसे दस्तावेज़ शामिल नहीं होंगे जिनमें दिए गए फ़ील्ड मौजूद नहीं हैं.
orderBy और मौजूदगी
किसी फ़ील्ड के हिसाब से क्वेरी को क्रम में लगाने पर, क्वेरी सिर्फ़ उन दस्तावेज़ों को दिखा सकती है जिनमें order-by फ़ील्ड मौजूद होता है.
उदाहरण के लिए, नीचे दी गई क्वेरी से ऐसे दस्तावेज़ नहीं दिखेंगे जिनमें 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”);