Catch up on highlights from Firebase at Google I/O 2023. Learn more

สั่งซื้อและจำกัดข้อมูลด้วย Cloud Firestore

Cloud Firestore มีฟังก์ชันการสืบค้นที่มีประสิทธิภาพสำหรับการระบุเอกสารที่คุณต้องการดึงจากคอลเลกชัน ข้อความค้นหาเหล่านี้ยังสามารถใช้กับ get() หรือ addSnapshotListener() ตามที่อธิบายไว้ใน Get Data

สั่งซื้อและจำกัดข้อมูล

ตามค่าเริ่มต้น คิวรีจะดึงเอกสารทั้งหมดที่ตรงกับคิวรีในลำดับจากน้อยไปมากตาม ID เอกสาร คุณสามารถระบุลำดับการจัดเรียงสำหรับข้อมูลของคุณโดยใช้ 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);
สวิฟต์
หมายเหตุ: ผลิตภัณฑ์นี้ไม่พร้อมใช้งานบนเป้าหมาย watchOS และ App Clip
citiesRef.order(by: "name").limit(to: 3)
วัตถุประสงค์-C
หมายเหตุ: ผลิตภัณฑ์นี้ไม่พร้อมใช้งานบนเป้าหมาย watchOS และ App Clip
[[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);
โหนด js
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)
พี.เอช.พี

พี.เอช.พี

สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งและการสร้างไคลเอ็นต์ Cloud Firestore โปรดดูที่ Cloud Firestore Client Libraries

$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);
สวิฟต์
หมายเหตุ: ผลิตภัณฑ์นี้ไม่พร้อมใช้งานบนเป้าหมาย watchOS และ App Clip
citiesRef.order(by: "name", descending: true).limit(to: 3)
วัตถุประสงค์-C
หมายเหตุ: ผลิตภัณฑ์นี้ไม่พร้อมใช้งานบนเป้าหมาย watchOS และ App Clip
[[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);
โหนด js
const lastThreeRes = await citiesRef.orderBy('name', 'desc').limit(3).get();
ไป
query := cities.OrderBy("name", firestore.Desc).Limit(3)
พี.เอช.พี

พี.เอช.พี

สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งและการสร้างไคลเอ็นต์ Cloud Firestore โปรดดูที่ Cloud Firestore Client Libraries

$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");
สวิฟต์
หมายเหตุ: ผลิตภัณฑ์นี้ไม่พร้อมใช้งานบนเป้าหมาย watchOS และ App Clip
citiesRef
    .order(by: "state")
    .order(by: "population", descending: true)
วัตถุประสงค์-C
หมายเหตุ: ผลิตภัณฑ์นี้ไม่พร้อมใช้งานบนเป้าหมาย watchOS และ App Clip
[[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);
โหนด js
const byStateByPopRes = await citiesRef.orderBy('state').orderBy('population', 'desc').get();
ไป
query := client.Collection("cities").OrderBy("state", firestore.Asc).OrderBy("population", firestore.Desc)
พี.เอช.พี

พี.เอช.พี

สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งและการสร้างไคลเอ็นต์ Cloud Firestore โปรดดูที่ Cloud Firestore Client Libraries

$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);
สวิฟต์
หมายเหตุ: ผลิตภัณฑ์นี้ไม่พร้อมใช้งานบนเป้าหมาย watchOS และ App Clip
citiesRef
    .whereField("population", isGreaterThan: 100000)
    .order(by: "population")
    .limit(to: 2)
วัตถุประสงค์-C
หมายเหตุ: ผลิตภัณฑ์นี้ไม่พร้อมใช้งานบนเป้าหมาย watchOS และ App Clip
[[[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(
    'population', '>', 2500000).order_by('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);
โหนด js
const biggestRes = await citiesRef.where('population', '>', 2500000)
  .orderBy('population').limit(2).get();
ไป
query := cities.Where("population", ">", 2500000).OrderBy("population", firestore.Desc).Limit(2)
พี.เอช.พี

พี.เอช.พี

สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งและการสร้างไคลเอ็นต์ Cloud Firestore โปรดดูที่ Cloud Firestore Client Libraries

$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");
    สวิฟต์
    หมายเหตุ: ผลิตภัณฑ์นี้ไม่พร้อมใช้งานบนเป้าหมาย watchOS และ App Clip
    citiesRef
        .whereField("population", isGreaterThan: 100000)
        .order(by: "population")
    วัตถุประสงค์-C
    หมายเหตุ: ผลิตภัณฑ์นี้ไม่พร้อมใช้งานบนเป้าหมาย watchOS และ App Clip
    [[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(
        'population', '>', 2500000).order_by('population')
    results = query.stream()

    Python

    cities_ref = db.collection("cities")
    query = cities_ref.where("population", ">", 2500000).order_by("population")
    results = query.stream()
    โหนด js
    citiesRef.where('population', '>', 2500000).orderBy('population');
    ไป
    query := cities.Where("population", ">", 2500000).OrderBy("population", firestore.Asc)
    พี.เอช.พี

    พี.เอช.พี

    สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งและการสร้างไคลเอ็นต์ Cloud Firestore โปรดดูที่ Cloud Firestore Client Libraries

    $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");
    สวิฟต์
    หมายเหตุ: ผลิตภัณฑ์นี้ไม่พร้อมใช้งานบนเป้าหมาย watchOS และ App Clip
    citiesRef
        .whereField("population", isGreaterThan: 100000)
        .order(by: "country")
    วัตถุประสงค์-C
    หมายเหตุ: ผลิตภัณฑ์นี้ไม่พร้อมใช้งานบนเป้าหมาย watchOS และ App Clip
    [[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('population', '>', 2500000).order_by('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");
    โหนด js
    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)
    พี.เอช.พี

    พี.เอช.พี

    สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการติดตั้งและการสร้างไคลเอ็นต์ Cloud Firestore โปรดดูที่ Cloud Firestore Client Libraries

    $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 อนุประโยค

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”);