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+KTX
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+KTX
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+KTX
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+KTX
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
फ़ील्ड के बिना कोई दस्तावेज़ नहीं दिखाती है, भले ही उस दस्तावेज़ में 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”);