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
और उसके अस्तित्व के बारे में
किसी फ़ील्ड के हिसाब से क्वेरी को क्रम से लगाने पर, क्वेरी सिर्फ़ उन दस्तावेज़ों को दिखा सकती है जिनमें क्रम से लगाने वाला फ़ील्ड मौजूद है.
उदाहरण के लिए, इस क्वेरी से ऐसे दस्तावेज़ नहीं मिलेंगे जिनमें population
फ़ील्ड सेट नहीं है. भले ही, वे क्वेरी फ़िल्टर की अन्य शर्तों को पूरा करते हों.
Java
db.collection("cities").whereEqualTo("country", “USA”).orderBy(“population”);
इसी तरह का एक और इफ़ेक्ट, असमानताओं पर लागू होता है. किसी फ़ील्ड पर असमानता फ़िल्टर वाली क्वेरी का मतलब यह भी है कि उस फ़ील्ड के हिसाब से क्रम से लगाया गया है. नीचे दी गई क्वेरी, 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”);