क्लाउड फायरस्टोर यह निर्दिष्ट करने के लिए शक्तिशाली क्वेरी कार्यक्षमता प्रदान करता है कि आप संग्रह से कौन से दस्तावेज़ पुनर्प्राप्त करना चाहते हैं। इन प्रश्नों का उपयोग get()
या addSnapshotListener()
के साथ भी किया जा सकता है, जैसा कि Get Data में वर्णित है।
आदेश और डेटा सीमित करें
डिफ़ॉल्ट रूप से, एक क्वेरी दस्तावेज़ आईडी द्वारा आरोही क्रम में क्वेरी को संतुष्ट करने वाले सभी दस्तावेज़ों को पुनः प्राप्त करती है। आप orderBy()
का उपयोग करके अपने डेटा के लिए सॉर्ट ऑर्डर निर्दिष्ट कर सकते हैं, और आप limit()
का उपयोग करके पुनर्प्राप्त किए गए दस्तावेज़ों की संख्या को सीमित कर सकते हैं।
उदाहरण के लिए, आप पहले 3 शहरों के लिए वर्णानुक्रम में क्वेरी कर सकते हैं:
Web version 9
import { query, orderBy, limit } from "firebase/firestore"; const q = query(citiesRef, orderBy("name"), limit(3));
Web version 8
citiesRef.orderBy("name").limit(3);
तीव्र
citiesRef.order(by: "name").limit(to: 3)
उद्देश्य सी
[[citiesRef queryOrderedByField:@"name"] queryLimitedTo:3];
Java
citiesRef.orderBy("name").limit(3);
Kotlin+KTX
citiesRef.orderBy("name").limit(3)
Dart
final citiesRef = db.collection("cities"); citiesRef.orderBy("name").limit(3);
जावा
अजगर
Python
सी++
cities_ref.OrderBy("name").Limit(3);
Node.js
जाना
पीएचपी
$query = $citiesRef->orderBy('name')->limit(3);
एकता
Query query = citiesRef.OrderBy("Name").Limit(3);
सी#
Query query = citiesRef.OrderBy("Name").Limit(3);
माणिक
आप अंतिम 3 शहरों को प्राप्त करने के लिए अवरोही क्रम में भी छाँट सकते हैं:
Web version 9
import { query, orderBy, limit } from "firebase/firestore"; const q = query(citiesRef, orderBy("name", "desc"), limit(3));
Web version 8
citiesRef.orderBy("name", "desc").limit(3);
तीव्र
citiesRef.order(by: "name", descending: true).limit(to: 3)
उद्देश्य सी
[[citiesRef queryOrderedByField:@"name" descending:YES] queryLimitedTo:3];
Java
citiesRef.orderBy("name", Direction.DESCENDING).limit(3);
Kotlin+KTX
citiesRef.orderBy("name", Query.Direction.DESCENDING).limit(3)
Dart
final citiesRef = db.collection("cities"); citiesRef.orderBy("name", descending: true).limit(3);
जावा
अजगर
Python
सी++
cities_ref.OrderBy("name", Query::Direction::kDescending).Limit(3);
Node.js
जाना
पीएचपी
$query = $citiesRef->orderBy('name', 'DESC')->limit(3);
एकता
Query query = citiesRef.OrderByDescending("Name").Limit(3);
सी#
Query query = citiesRef.OrderByDescending("Name").Limit(3);
माणिक
आप कई क्षेत्रों द्वारा भी ऑर्डर कर सकते हैं। उदाहरण के लिए, यदि आप राज्य द्वारा और प्रत्येक राज्य के भीतर जनसंख्या द्वारा अवरोही क्रम में आदेश देना चाहते हैं:
Web version 9
import { query, orderBy } from "firebase/firestore"; const q = query(citiesRef, orderBy("state"), orderBy("population", "desc"));
Web version 8
citiesRef.orderBy("state").orderBy("population", "desc");
तीव्र
citiesRef .order(by: "state") .order(by: "population", descending: true)
उद्देश्य सी
[[citiesRef queryOrderedByField:@"state"] queryOrderedByField:@"population" descending:YES];
Java
citiesRef.orderBy("state").orderBy("population", Direction.DESCENDING);
Kotlin+KTX
citiesRef.orderBy("state").orderBy("population", Query.Direction.DESCENDING)
Dart
final citiesRef = db.collection("cities"); citiesRef.orderBy("state").orderBy("population", descending: true);
जावा
अजगर
Python
सी++
cities_ref.OrderBy("state").OrderBy("name", Query::Direction::kDescending);
Node.js
जाना
पीएचपी
$query = $citiesRef->orderBy('state')->orderBy('population', 'DESC');
एकता
Query query = citiesRef.OrderBy("State").OrderByDescending("Population");
सी#
Query query = citiesRef.OrderBy("State").OrderByDescending("Population");
माणिक
आप where()
फ़िल्टर को orderBy()
और limit()
के साथ जोड़ सकते हैं। निम्नलिखित उदाहरण में, क्वेरी जनसंख्या सीमा को परिभाषित करती हैं, जनसंख्या द्वारा आरोही क्रम में क्रमबद्ध करती हैं, और केवल पहले कुछ परिणाम देती हैं जो सीमा से अधिक हैं:
Web version 9
import { query, where, orderBy, limit } from "firebase/firestore"; const q = query(citiesRef, where("population", ">", 100000), orderBy("population"), limit(2));
Web version 8
citiesRef.where("population", ">", 100000).orderBy("population").limit(2);
तीव्र
citiesRef .whereField("population", isGreaterThan: 100000) .order(by: "population") .limit(to: 2)
उद्देश्य सी
[[[citiesRef queryWhereField:@"population" isGreaterThan:@100000] queryOrderedByField:@"population"] queryLimitedTo:2];
Java
citiesRef.whereGreaterThan("population", 100000).orderBy("population").limit(2);
Kotlin+KTX
citiesRef.whereGreaterThan("population", 100000).orderBy("population").limit(2)
Dart
final citiesRef = db.collection("cities"); citiesRef .where("population", isGreaterThan: 100000) .orderBy("population") .limit(2);
जावा
अजगर
Python
सी++
cities_ref.WhereGreaterThan("population", FieldValue::Integer(100000)) .OrderBy("population") .Limit(2);
Node.js
जाना
पीएचपी
$query = $citiesRef ->where('population', '>', 2500000) ->orderBy('population') ->limit(2);
एकता
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Population") .Limit(2);
सी#
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Population") .Limit(2);
माणिक
हालांकि, यदि आपके पास श्रेणी तुलना ( <
, <=
, >
, >=
) वाला फ़िल्टर है, तो आपका पहला ऑर्डर उसी फ़ील्ड पर होना चाहिए, नीचे orderBy()
सीमाओं की सूची देखें।
सीमाओं
orderBy()
क्लॉज के लिए निम्नलिखित प्रतिबंधों पर ध्यान दें:
- एक
orderBy()
क्लॉज दिए गए फ़ील्ड के अस्तित्व के लिए भी फ़िल्टर करता है। परिणाम सेट में ऐसे दस्तावेज़ शामिल नहीं होंगे जिनमें दिए गए फ़ील्ड शामिल नहीं हैं। यदि आप श्रेणी तुलना (
<
,<=
,>
,>=
) के साथ फ़िल्टर शामिल करते हैं, तो आपका पहला आदेश उसी फ़ील्ड पर होना चाहिए:मान्य : रेंज फ़िल्टर और
orderBy
एक ही फ़ील्ड परWeb version 9
import { query, where, orderBy } from "firebase/firestore"; const q = query(citiesRef, where("population", ">", 100000), orderBy("population"));
Web version 8
citiesRef.where("population", ">", 100000).orderBy("population");
तीव्र
नोट: यह उत्पाद watchOS और ऐप क्लिप लक्ष्य पर उपलब्ध नहीं है।citiesRef .whereField("population", isGreaterThan: 100000) .order(by: "population")
उद्देश्य सी
नोट: यह उत्पाद watchOS और ऐप क्लिप लक्ष्य पर उपलब्ध नहीं है।[[citiesRef queryWhereField:@"population" isGreaterThan:@100000] queryOrderedByField:@"population"];
Java
citiesRef.whereGreaterThan("population", 100000).orderBy("population");
Kotlin+KTX
citiesRef.whereGreaterThan("population", 100000).orderBy("population")
Dart
final citiesRef = db.collection("cities"); citiesRef.where("population", isGreaterThan: 100000).orderBy("population");
जावा
Query query = cities.whereGreaterThan("population", 2500000L).orderBy("population");
अजगर
Python
Node.js
जाना
पीएचपी
$query = $citiesRef ->where('population', '>', 2500000) ->orderBy('population');
एकता
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Population");
सी#
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Population");
माणिक
अमान्य : रेंज फिल्टर और पहला
orderBy
विभिन्न क्षेत्रों परWeb version 9
import { query, where, orderBy } from "firebase/firestore"; const q = query(citiesRef, where("population", ">", 100000), orderBy("country"));
Web version 8
citiesRef.where("population", ">", 100000).orderBy("country");
तीव्र
नोट: यह उत्पाद watchOS और ऐप क्लिप लक्ष्य पर उपलब्ध नहीं है।citiesRef .whereField("population", isGreaterThan: 100000) .order(by: "country")
उद्देश्य सी
नोट: यह उत्पाद watchOS और ऐप क्लिप लक्ष्य पर उपलब्ध नहीं है।[[citiesRef queryWhereField:@"population" isGreaterThan:@100000] queryOrderedByField:@"country"];
Java
citiesRef.whereGreaterThan("population", 100000).orderBy("country");
Kotlin+KTX
citiesRef.whereGreaterThan("population", 100000).orderBy("country")
Dart
final citiesRef = db.collection("cities"); citiesRef.where("population", isGreaterThan: 100000).orderBy("country");
जावा
अजगर
Python
सी++
// BAD EXAMPLE -- will crash the program: cities_ref.WhereGreaterThan("population", FieldValue::Integer(100000)) .OrderBy("country");
Node.js
जाना
पीएचपी
$invalidRangeQuery = $citiesRef ->where('population', '>', 2500000) ->orderBy('country');
एकता
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Country");
सी#
Query query = citiesRef .WhereGreaterThan("Population", 2500000) .OrderBy("Country");
माणिक
- आप समानता (
=
) या खंडin
शामिल किसी भी क्षेत्र द्वारा अपनी क्वेरी का आदेश नहीं दे सकते।