क्लाउड फायरस्टोर के साथ डेटा ऑर्डर करें और सीमित करें

क्लाउड फायरस्टोर यह निर्दिष्ट करने के लिए शक्तिशाली क्वेरी कार्यक्षमता प्रदान करता है कि आप किसी संग्रह से कौन से दस्तावेज़ पुनर्प्राप्त करना चाहते हैं। इन क्वेरीज़ का उपयोग get() या addSnapshotListener() के साथ भी किया जा सकता है, जैसा कि डेटा प्राप्त करें में वर्णित है।

डेटा ऑर्डर करें और सीमित करें

डिफ़ॉल्ट रूप से, एक क्वेरी उन सभी दस्तावेज़ों को पुनः प्राप्त करती है जो दस्तावेज़ आईडी द्वारा आरोही क्रम में क्वेरी को संतुष्ट करते हैं। आप orderBy() उपयोग करके अपने डेटा के लिए सॉर्ट ऑर्डर निर्दिष्ट कर सकते हैं, और आप limit() का उपयोग करके पुनर्प्राप्त दस्तावेज़ों की संख्या को सीमित कर सकते हैं। यदि आप एक limit() निर्दिष्ट करते हैं, तो मान शून्य से अधिक या उसके बराबर होना चाहिए।

उदाहरण के लिए, आप पहले 3 शहरों के लिए वर्णानुक्रम में क्वेरी कर सकते हैं:

Web modular API

import { query, orderBy, limit } from "firebase/firestore";  

const q = query(citiesRef, orderBy("name"), limit(3));

Web namespaced API

citiesRef.orderBy("name").limit(3);
तीव्र
नोट: यह उत्पाद वॉचओएस और ऐप क्लिप लक्ष्य पर उपलब्ध नहीं है।
citiesRef.order(by: "name").limit(to: 3)
उद्देश्य सी
नोट: यह उत्पाद वॉचओएस और ऐप क्लिप लक्ष्य पर उपलब्ध नहीं है।
[[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);
जावा
Query query = cities.orderBy("name").limit(3);
Query query = cities.orderBy("name").limitToLast(3);
अजगर
cities_ref = db.collection("cities")
query = cities_ref.order_by("name").limit_to_last(2)
results = query.get()

Python

cities_ref = db.collection("cities")
query = cities_ref.order_by("name").limit_to_last(2)
results = await query.get()
सी++
cities_ref.OrderBy("name").Limit(3);
नोड.जे.एस
const firstThreeRes = await citiesRef.orderBy('name').limit(3).get();
जाना
query := cities.OrderBy("name", firestore.Asc).Limit(3)
query := cities.OrderBy("name", firestore.Asc).LimitToLast(3)
पीएचपी

पीएचपी

क्लाउड फायरस्टोर क्लाइंट स्थापित करने और बनाने के बारे में अधिक जानकारी के लिए, क्लाउड फायरस्टोर क्लाइंट लाइब्रेरीज़ देखें।

$query = $citiesRef->orderBy('name')->limit(3);
एकता
Query query = citiesRef.OrderBy("Name").Limit(3);
सी#
Query query = citiesRef.OrderBy("Name").Limit(3);
माणिक
query = cities_ref.order("name").limit(3)

आप अंतिम 3 शहरों को प्राप्त करने के लिए अवरोही क्रम में भी क्रमबद्ध कर सकते हैं:

Web modular API

import { query, orderBy, limit } from "firebase/firestore";  

const q = query(citiesRef, orderBy("name", "desc"), limit(3));

Web namespaced API

citiesRef.orderBy("name", "desc").limit(3);
तीव्र
नोट: यह उत्पाद वॉचओएस और ऐप क्लिप लक्ष्य पर उपलब्ध नहीं है।
citiesRef.order(by: "name", descending: true).limit(to: 3)
उद्देश्य सी
नोट: यह उत्पाद वॉचओएस और ऐप क्लिप लक्ष्य पर उपलब्ध नहीं है।
[[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);
जावा
Query query = cities.orderBy("name", Direction.DESCENDING).limit(3);
अजगर
cities_ref = db.collection("cities")
query = cities_ref.order_by("name", direction=firestore.Query.DESCENDING).limit(3)
results = query.stream()

Python

cities_ref = db.collection("cities")
query = cities_ref.order_by("name", direction=firestore.Query.DESCENDING).limit(3)
results = query.stream()
सी++
cities_ref.OrderBy("name", Query::Direction::kDescending).Limit(3);
नोड.जे.एस
const lastThreeRes = await citiesRef.orderBy('name', 'desc').limit(3).get();
जाना
query := cities.OrderBy("name", firestore.Desc).Limit(3)
पीएचपी

पीएचपी

क्लाउड फायरस्टोर क्लाइंट स्थापित करने और बनाने के बारे में अधिक जानकारी के लिए, क्लाउड फायरस्टोर क्लाइंट लाइब्रेरीज़ देखें।

$query = $citiesRef->orderBy('name', 'DESC')->limit(3);
एकता
Query query = citiesRef.OrderByDescending("Name").Limit(3);
सी#
Query query = citiesRef.OrderByDescending("Name").Limit(3);
माणिक
query = cities_ref.order("name", "desc").limit(3)

आप एकाधिक फ़ील्ड के आधार पर भी ऑर्डर कर सकते हैं. उदाहरण के लिए, यदि आप राज्य के अनुसार और प्रत्येक राज्य के भीतर जनसंख्या के अनुसार अवरोही क्रम में ऑर्डर करना चाहते हैं:

Web modular API

import { query, orderBy } from "firebase/firestore";  

const q = query(citiesRef, orderBy("state"), orderBy("population", "desc"));

Web namespaced API

citiesRef.orderBy("state").orderBy("population", "desc");
तीव्र
नोट: यह उत्पाद वॉचओएस और ऐप क्लिप लक्ष्य पर उपलब्ध नहीं है।
citiesRef
  .order(by: "state")
  .order(by: "population", descending: true)
उद्देश्य सी
नोट: यह उत्पाद वॉचओएस और ऐप क्लिप लक्ष्य पर उपलब्ध नहीं है।
[[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);
जावा
Query query = cities.orderBy("state").orderBy("population", Direction.DESCENDING);
अजगर
cities_ref = db.collection("cities")
cities_ref.order_by("state").order_by(
    "population", direction=firestore.Query.DESCENDING
)

Python

cities_ref = db.collection("cities")
cities_ref.order_by("state").order_by(
    "population", direction=firestore.Query.DESCENDING
)
सी++
cities_ref.OrderBy("state").OrderBy("name", Query::Direction::kDescending);
नोड.जे.एस
const byStateByPopRes = await citiesRef.orderBy('state').orderBy('population', 'desc').get();
जाना
query := client.Collection("cities").OrderBy("state", firestore.Asc).OrderBy("population", firestore.Desc)
पीएचपी

पीएचपी

क्लाउड फायरस्टोर क्लाइंट स्थापित करने और बनाने के बारे में अधिक जानकारी के लिए, क्लाउड फायरस्टोर क्लाइंट लाइब्रेरीज़ देखें।

$query = $citiesRef->orderBy('state')->orderBy('population', 'DESC');
एकता
Query query = citiesRef.OrderBy("State").OrderByDescending("Population");
सी#
Query query = citiesRef.OrderBy("State").OrderByDescending("Population");
माणिक
query = cities_ref.order("state").order("population", "desc")

आप where() फ़िल्टर को orderBy() और limit() के साथ जोड़ सकते हैं। निम्नलिखित उदाहरण में, प्रश्न जनसंख्या सीमा को परिभाषित करते हैं, जनसंख्या के आधार पर आरोही क्रम में क्रमबद्ध करते हैं, और केवल पहले कुछ परिणाम लौटाते हैं जो सीमा से अधिक होते हैं:

Web modular API

import { query, where, orderBy, limit } from "firebase/firestore";  

const q = query(citiesRef, where("population", ">", 100000), orderBy("population"), limit(2));

Web namespaced API

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];

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);
जावा
Query query = cities.whereGreaterThan("population", 2500000L).orderBy("population").limit(2);
अजगर
cities_ref = db.collection("cities")
query = (
    cities_ref.where(filter=FieldFilter("population", ">", 2500000))
    .order_by("population")
    .limit(2)
)
results = query.stream()

Python

cities_ref = db.collection("cities")
query = (
    cities_ref.where(filter=FieldFilter("population", ">", 2500000))
    .order_by("population")
    .limit(2)
)
results = query.stream()
सी++
cities_ref.WhereGreaterThan("population", FieldValue::Integer(100000))
    .OrderBy("population")
    .Limit(2);
नोड.जे.एस
const biggestRes = await citiesRef.where('population', '>', 2500000)
  .orderBy('population').limit(2).get();
जाना
query := cities.Where("population", ">", 2500000).OrderBy("population", firestore.Desc).Limit(2)
पीएचपी

पीएचपी

क्लाउड फायरस्टोर क्लाइंट स्थापित करने और बनाने के बारे में अधिक जानकारी के लिए, क्लाउड फायरस्टोर क्लाइंट लाइब्रेरीज़ देखें।

$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);
माणिक
query = cities_ref.where("population", ">", 2_500_000).order("population").limit(2)

हालाँकि, यदि आपके पास श्रेणी तुलना ( < , <= , > , >= ) वाला फ़िल्टर है, तो आपका पहला ऑर्डर उसी फ़ील्ड पर होना चाहिए, नीचे orderBy() सीमाओं की सूची देखें।

सीमाएँ

orderBy() क्लॉज के लिए निम्नलिखित प्रतिबंधों पर ध्यान दें:

  • एक orderBy() क्लॉज दिए गए फ़ील्ड के अस्तित्व को भी फ़िल्टर करता है । परिणाम सेट में वे दस्तावेज़ शामिल नहीं होंगे जिनमें दिए गए फ़ील्ड शामिल नहीं हैं।
  • यदि आप श्रेणी तुलना ( < , <= , > , >= ) के साथ एक फ़िल्टर शामिल करते हैं, तो आपका पहला ऑर्डर उसी फ़ील्ड पर होना चाहिए:

    मान्य : एक ही फ़ील्ड पर रेंज फ़िल्टर और orderBy

    Web modular API

    import { query, where, orderBy } from "firebase/firestore";  
    
    const q = query(citiesRef, where("population", ">", 100000), orderBy("population"));

    Web namespaced API

    citiesRef.where("population", ">", 100000).orderBy("population");
    तीव्र
    नोट: यह उत्पाद वॉचओएस और ऐप क्लिप लक्ष्य पर उपलब्ध नहीं है।
    citiesRef
      .whereField("population", isGreaterThan: 100000)
      .order(by: "population")
    उद्देश्य सी
    नोट: यह उत्पाद वॉचओएस और ऐप क्लिप लक्ष्य पर उपलब्ध नहीं है।
    [[citiesRef queryWhereField:@"population" isGreaterThan:@100000]
        queryOrderedByField:@"population"];

    Kotlin+KTX

    citiesRef.whereGreaterThan("population", 100000).orderBy("population")

    Java

    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");
    अजगर
    cities_ref = db.collection("cities")
    query = cities_ref.where(filter=FieldFilter("population", ">", 2500000)).order_by(
        "population"
    )
    results = query.stream()

    Python

    cities_ref = db.collection("cities")
    query = cities_ref.where(filter=FieldFilter("population", ">", 2500000)).order_by(
        "population"
    )
    results = query.stream()
    नोड.जे.एस
    citiesRef.where('population', '>', 2500000).orderBy('population');
    जाना
    query := cities.Where("population", ">", 2500000).OrderBy("population", firestore.Asc)
    पीएचपी

    पीएचपी

    क्लाउड फायरस्टोर क्लाइंट स्थापित करने और बनाने के बारे में अधिक जानकारी के लिए, क्लाउड फायरस्टोर क्लाइंट लाइब्रेरीज़ देखें।

    $query = $citiesRef
        ->where('population', '>', 2500000)
        ->orderBy('population');
    एकता
    Query query = citiesRef
        .WhereGreaterThan("Population", 2500000)
        .OrderBy("Population");
    सी#
    Query query = citiesRef
        .WhereGreaterThan("Population", 2500000)
        .OrderBy("Population");
    माणिक
    query = cities_ref.where("population", ">", 2_500_000).order("population")

    अमान्य : विभिन्न फ़ील्ड पर रेंज फ़िल्टर और प्रथम orderBy

    Web modular API

    import { query, where, orderBy } from "firebase/firestore";  
    
    const q = query(citiesRef, where("population", ">", 100000), orderBy("country"));

    Web namespaced API

    citiesRef.where("population", ">", 100000).orderBy("country");
    तीव्र
    नोट: यह उत्पाद वॉचओएस और ऐप क्लिप लक्ष्य पर उपलब्ध नहीं है।
    citiesRef
      .whereField("population", isGreaterThan: 100000)
      .order(by: "country")
    उद्देश्य सी
    नोट: यह उत्पाद वॉचओएस और ऐप क्लिप लक्ष्य पर उपलब्ध नहीं है।
    [[citiesRef queryWhereField:@"population" isGreaterThan:@100000] queryOrderedByField:@"country"];

    Kotlin+KTX

    citiesRef.whereGreaterThan("population", 100000).orderBy("country")

    Java

    citiesRef.whereGreaterThan("population", 100000).orderBy("country");

    Dart

    final citiesRef = db.collection("cities");
    citiesRef.where("population", isGreaterThan: 100000).orderBy("country");
    जावा
    Query query = cities.whereGreaterThan("population", 2500000L).orderBy("country");
    अजगर
    cities_ref = db.collection("cities")
    query = cities_ref.where(filter=FieldFilter("population", ">", 2500000)).order_by(
        "country"
    )
    results = query.stream()

    Python

    cities_ref = db.collection("cities")
    query = cities_ref.where(filter=FieldFilter("population", ">", 2500000)).order_by(
        "country"
    )
    results = query.stream()
    सी++
    // BAD EXAMPLE -- will crash the program:
    cities_ref.WhereGreaterThan("population", FieldValue::Integer(100000))
        .OrderBy("country");
    नोड.जे.एस
    citiesRef.where('population', '>', 2500000).orderBy('country');
    जाना
    // Note: This is an invalid query. It violates the constraint that range
    // and order by are required to be on the same field.
    query := cities.Where("population", ">", 2500000).OrderBy("country", firestore.Asc)
    पीएचपी

    पीएचपी

    क्लाउड फायरस्टोर क्लाइंट स्थापित करने और बनाने के बारे में अधिक जानकारी के लिए, क्लाउड फायरस्टोर क्लाइंट लाइब्रेरीज़ देखें।

    $invalidRangeQuery = $citiesRef
        ->where('population', '>', 2500000)
        ->orderBy('country');
    एकता
    Query query = citiesRef
        .WhereGreaterThan("Population", 2500000)
        .OrderBy("Country");
    सी#
    Query query = citiesRef
        .WhereGreaterThan("Population", 2500000)
        .OrderBy("Country");
    माणिक
    query = cities_ref.where("population", ">", 2_500_000).order("country")

orderBy और अस्तित्व

जब आप किसी दिए गए फ़ील्ड द्वारा क्वेरी ऑर्डर करते हैं, तो क्वेरी केवल उन दस्तावेज़ों को वापस कर सकती है जहां ऑर्डर-बाय फ़ील्ड मौजूद है।

उदाहरण के लिए, निम्नलिखित क्वेरी कोई भी दस्तावेज़ नहीं लौटाएगी जहां population फ़ील्ड सेट नहीं है, भले ही वे अन्यथा क्वेरी फ़िल्टर को पूरा करते हों।

जावा
db.collection("cities").whereEqualTo("country", “USA”).orderBy(“population”);

एक संबंधित प्रभाव असमानताओं पर लागू होता है। किसी फ़ील्ड पर असमानता फ़िल्टर वाली क्वेरी का तात्पर्य उस फ़ील्ड द्वारा ऑर्डर करने से भी है। निम्न क्वेरी population फ़ील्ड के बिना दस्तावेज़ नहीं लौटाती, भले ही उस दस्तावेज़ में country = USA । वैकल्पिक समाधान के रूप में, आप प्रत्येक ऑर्डर के लिए अलग-अलग क्वेरी निष्पादित कर सकते हैं या आप उन सभी फ़ील्ड के लिए एक मान निर्दिष्ट कर सकते हैं जिनके द्वारा आप ऑर्डर करते हैं।

जावा
db.collection(“cities”).where(or(“country”, USA”), greaterThan(“population”, 250000));

उपरोक्त क्वेरी में असमानता पर एक निहित आदेश शामिल है और यह निम्नलिखित के बराबर है:

जावा
db.collection(“cities”).where(or(“country”, USA”), greaterThan(“population”, 250000)).orderBy(“population”);