Join us for Firebase Summit on November 10, 2021. Tune in to learn how Firebase can help you accelerate app development, release with confidence, and scale with ease. Register

Cloud Firestore ile verileri sipariş edin ve sınırlayın

Cloud Firestore, bir koleksiyondan hangi belgeleri almak istediğinizi belirtmek için güçlü sorgu işlevi sağlar. Bu sorgular da ikisinden biri ile kullanılabilir get() veya addSnapshotListener() , anlatıldığı gibi Veri Al .

Verileri sipariş et ve sınırla

Varsayılan olarak bir sorgu, sorguyu karşılayan tüm belgeleri belge kimliğine göre artan sırada alır. Sen kullanarak veri için sıralama düzeni belirtebilirsiniz orderBy() ve doküman sayısı kullanılarak alınan sınırlayabilir limit() .

Örneğin, ilk 3 şehri aşağıdakilerle alfabetik olarak sorgulayabilirsiniz:

Web sürümü 9

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

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

Web sürümü 8

citiesRef.orderBy("name").limit(3);
Süratli
citiesRef.order(by: "name").limit(to: 3)
Amaç-C
[[citiesRef queryOrderedByField:@"name"] queryLimitedTo:3];

Java

citiesRef.orderBy("name").limit(3);

Kotlin+KTX

citiesRef.orderBy("name").limit(3)
Java
Query query = cities.orderBy("name").limit(3);
Query query = cities.orderBy("name").limitToLast(3);
piton
cities_ref = db.collection("cities")
query = cities_ref.order_by("name").limit_to_last(2)
results = query.get()

piton

cities_ref = db.collection("cities")
query = cities_ref.order_by("name").limit_to_last(2)
results = await query.get()
C++
cities_ref.OrderBy("name").Limit(3);
Node.js
const firstThreeRes = await citiesRef.orderBy('name').limit(3).get();
Gitmek
query := cities.OrderBy("name", firestore.Asc).Limit(3)
query := cities.OrderBy("name", firestore.Asc).LimitToLast(3)
PHP
$query = $citiesRef->orderBy('name')->limit(3);
Birlik
Query query = citiesRef.OrderBy("Name").Limit(3);
C#
Query query = citiesRef.OrderBy("Name").Limit(3);
yakut
query = cities_ref.order("name").limit(3)

Sort son 3 şehirler almak için azalan düzende de olabilir:

Web sürümü 9

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

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

Web sürümü 8

citiesRef.orderBy("name", "desc").limit(3);
Süratli
citiesRef.order(by: "name", descending: true).limit(to: 3)
Amaç-C
[[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)
Java
Query query = cities.orderBy("name", Direction.DESCENDING).limit(3);
piton
cities_ref = db.collection(u'cities')
query = cities_ref.order_by(
    u'name', direction=firestore.Query.DESCENDING).limit(3)
results = query.stream()

piton

cities_ref = db.collection("cities")
query = cities_ref.order_by("name", direction=firestore.Query.DESCENDING).limit(3)
results = query.stream()
C++
cities_ref.OrderBy("name", Query::Direction::kDescending).Limit(3);
Node.js
const lastThreeRes = await citiesRef.orderBy('name', 'desc').limit(3).get();
Gitmek
query := cities.OrderBy("name", firestore.Desc).Limit(3)
PHP
$query = $citiesRef->orderBy('name', 'DESC')->limit(3);
Birlik
Query query = citiesRef.OrderByDescending("Name").Limit(3);
C#
Query query = citiesRef.OrderByDescending("Name").Limit(3);
yakut
query = cities_ref.order("name", "desc").limit(3)

Ayrıca birden fazla alana göre sipariş verebilirsiniz. Örneğin, eyalete göre ve her eyalette nüfusa göre azalan sırayla sıralamak istiyorsanız:

Web sürümü 9

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

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

Web sürümü 8

citiesRef.orderBy("state").orderBy("population", "desc");
Süratli
citiesRef
    .order(by: "state")
    .order(by: "population", descending: true)
Amaç-C
[[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)
Java
Query query = cities.orderBy("state").orderBy("population", Direction.DESCENDING);
piton
cities_ref = db.collection(u'cities')
cities_ref.order_by(u'state').order_by(
    u'population', direction=firestore.Query.DESCENDING)

piton

cities_ref = db.collection("cities")
cities_ref.order_by("state").order_by(
    "population", direction=firestore.Query.DESCENDING
)
C++
cities_ref.OrderBy("state").OrderBy("name", Query::Direction::kDescending);
Node.js
const byStateByPopRes = await citiesRef.orderBy('state').orderBy('population', 'desc').get();
Gitmek
query := client.Collection("cities").OrderBy("state", firestore.Asc).OrderBy("population", firestore.Desc)
PHP
$query = $citiesRef->orderBy('state')->orderBy('population', 'DESC');
Birlik
Query query = citiesRef.OrderBy("State").OrderByDescending("Population");
C#
Query query = citiesRef.OrderBy("State").OrderByDescending("Population");
yakut
query = cities_ref.order("state").order("population", "desc")

Birleştirebilirsiniz where() ile filtreler orderBy() ve limit() . Aşağıdaki örnekte, sorgular bir popülasyon eşiği tanımlar, popülasyona göre artan düzende sıralar ve yalnızca eşiği aşan ilk birkaç sonucu döndürür:

Web sürümü 9

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

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

Web sürümü 8

citiesRef.where("population", ">", 100000).orderBy("population").limit(2);
Süratli
citiesRef
    .whereField("population", isGreaterThan: 100000)
    .order(by: "population")
    .limit(to: 2)
Amaç-C
[[[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)
Java
Query query = cities.whereGreaterThan("population", 2500000L).orderBy("population").limit(2);
piton
cities_ref = db.collection(u'cities')
query = cities_ref.where(
    u'population', u'>', 2500000).order_by(u'population').limit(2)
results = query.stream()

piton

cities_ref = db.collection("cities")
query = cities_ref.where("population", ">", 2500000).order_by("population").limit(2)
results = query.stream()
C++
cities_ref.WhereGreaterThan("population", FieldValue::Integer(100000))
    .OrderBy("population")
    .Limit(2);
Node.js
const biggestRes = await citiesRef.where('population', '>', 2500000)
  .orderBy('population').limit(2).get();
Gitmek
query := cities.Where("population", ">", 2500000).OrderBy("population", firestore.Desc).Limit(2)
PHP
$query = $citiesRef
    ->where('population', '>', 2500000)
    ->orderBy('population')
    ->limit(2);
Birlik
Query query = citiesRef
    .WhereGreaterThan("Population", 2500000)
    .OrderBy("Population")
    .Limit(2);
C#
Query query = citiesRef
    .WhereGreaterThan("Population", 2500000)
    .OrderBy("Population")
    .Limit(2);
yakut
query = cities_ref.where("population", ">", 2_500_000).order("population").limit(2)

Ancak bir dizi karşılaştırma ile bir filtre varsa, ( < , <= , > , >= ), ilk sipariş, aynı alanda olmalıdır listesine bakın orderBy() aşağıda sınırlamaları.

sınırlamalar

Aşağıdaki kısıtlamalara dikkat orderBy() maddeleri:

  • Bir orderBy() hükmü de verilen alanların varlığı için filtreler. Sonuç kümesi, verilen alanları içermeyen belgeleri içermeyecektir.
  • Bir aralık karşılaştırma ile bir filtre eklerseniz ( < , <= , > , >= ), ilk sipariş aynı alanda olmalıdır:

    Geçerli: Menzil filtresi ve orderBy aynı sahada

    Web sürümü 9

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

    Web sürümü 8

    citiesRef.where("population", ">", 100000).orderBy("population");
    Süratli
    citiesRef
        .whereField("population", isGreaterThan: 100000)
        .order(by: "population")
    Amaç-C
    [[citiesRef queryWhereField:@"population" isGreaterThan:@100000]
        queryOrderedByField:@"population"];

    Java

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

    Kotlin+KTX

    citiesRef.whereGreaterThan("population", 100000).orderBy("population")
    Java
    Query query = cities.whereGreaterThan("population", 2500000L).orderBy("population");
    piton
    cities_ref = db.collection(u'cities')
    query = cities_ref.where(
        u'population', u'>', 2500000).order_by(u'population')
    results = query.stream()

    piton

    cities_ref = db.collection("cities")
    query = cities_ref.where("population", ">", 2500000).order_by("population")
    results = query.stream()
    Node.js
    citiesRef.where('population', '>', 2500000).orderBy('population');
    Gitmek
    query := cities.Where("population", ">", 2500000).OrderBy("population", firestore.Asc)
    PHP
    $query = $citiesRef
        ->where('population', '>', 2500000)
        ->orderBy('population');
    Birlik
    Query query = citiesRef
        .WhereGreaterThan("Population", 2500000)
        .OrderBy("Population");
    C#
    Query query = citiesRef
        .WhereGreaterThan("Population", 2500000)
        .OrderBy("Population");
    yakut
    query = cities_ref.where("population", ">", 2_500_000).order("population")

    Geçersiz: Menzil filtresi ve ilk orderBy farklı alanlarda

    Web sürümü 9

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

    Web sürümü 8

    citiesRef.where("population", ">", 100000).orderBy("country");
    Süratli
    citiesRef
        .whereField("population", isGreaterThan: 100000)
        .order(by: "country")
    Amaç-C
    [[citiesRef queryWhereField:@"population" isGreaterThan:@100000] queryOrderedByField:@"country"];

    Java

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

    Kotlin+KTX

    citiesRef.whereGreaterThan("population", 100000).orderBy("country")
    Java
    Query query = cities.whereGreaterThan("population", 2500000L).orderBy("country");
    piton
    cities_ref = db.collection(u'cities')
    query = cities_ref.where(u'population', u'>', 2500000).order_by(u'country')
    results = query.stream()

    piton

    cities_ref = db.collection("cities")
    query = cities_ref.where("population", ">", 2500000).order_by("country")
    results = query.stream()
    C++
    // BAD EXAMPLE -- will crash the program:
    cities_ref.WhereGreaterThan("population", FieldValue::Integer(100000))
        .OrderBy("country");
    Node.js
    citiesRef.where('population', '>', 2500000).orderBy('country');
    Gitmek
    // 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)
    PHP
    $invalidRangeQuery = $citiesRef
        ->where('population', '>', 2500000)
        ->orderBy('country');
    Birlik
    Query query = citiesRef
        .WhereGreaterThan("Population", 2500000)
        .OrderBy("Country");
    C#
    Query query = citiesRef
        .WhereGreaterThan("Population", 2500000)
        .OrderBy("Country");
    yakut
    query = cities_ref.where("population", ">", 2_500_000).order("country")
  • Herhangi bir eşitlik dahil alan (da sorgunuzu sipariş edemez = ) ya da in maddesi.