Firebase is back at Google I/O on May 10! Register now

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

संग्रह की मदद से व्यवस्थित रहें अपनी प्राथमिकताओं के आधार पर, कॉन्टेंट को सेव करें और कैटगरी में बांटें.

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

आदेश और डेटा सीमित करें

डिफ़ॉल्ट रूप से, एक क्वेरी उन सभी दस्तावेजों को पुनः प्राप्त करती है जो दस्तावेज़ आईडी द्वारा आरोही क्रम में क्वेरी को संतुष्ट करते हैं। आप 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];

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

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(u'cities')
query = cities_ref.order_by(
    u'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 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];

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(u'cities')
cities_ref.order_by(u'state').order_by(
    u'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 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];

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(u'cities')
query = cities_ref.where(
    u'population', u'>', 2500000).order_by(u'population').limit(2)
results = query.stream()

Python

cities_ref = db.collection("cities")
query = cities_ref.where("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 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");
    तीव्र
    नोट: यह उत्पाद वॉचओएस और एप क्लिप लक्ष्यों पर उपलब्ध नहीं है।
    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(u'cities')
    query = cities_ref.where(
        u'population', u'>', 2500000).order_by(u'population')
    results = query.stream()

    Python

    cities_ref = db.collection("cities")
    query = cities_ref.where("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 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");
    तीव्र
    नोट: यह उत्पाद वॉचओएस और एप क्लिप लक्ष्यों पर उपलब्ध नहीं है।
    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(u'cities')
    query = cities_ref.where(u'population', u'>', 2500000).order_by(u'country')
    results = query.stream()

    Python

    cities_ref = db.collection("cities")
    query = cities_ref.where("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")
  • आप समानता ( = ) या खंड in शामिल किसी भी क्षेत्र द्वारा अपनी क्वेरी का क्रम नहीं दे सकते।