使用範圍和不等式篩選器查詢多個欄位總覽

Firestore 支援在單一查詢中,對多個欄位使用範圍和不等式篩選器。您可以在多個欄位上設定範圍和不等式條件,並將後續篩選邏輯的實作作業委派給 Firestore,簡化應用程式開發作業。

針對多個欄位使用範圍和不等式篩選器

下列查詢會對人口和密度使用範圍篩選器,傳回人口超過 1,000,000 人,且每單位面積人口密度低於 10,000 人的所有城市。

網頁版 9 模組化

const q = query(
    collection(db, "cities"),
    where('population', '>', 1000000),
    where('density', '<', 10000),
  );

Swift

let query = db.collection("cities")
  .whereField("population", isGreaterThan: 1000000)
  .whereField("density", isLessThan: 10000)

Objective-C

FIRQuery *query =
 [[[[self.db collectionWithPath:@"cities"]
queryWhereField:@"population" isGreaterThan:@1000000]
   queryWhereField:@"density" isLessThan:@10000];

Java Android

Query query = db.collection("cities")
 .whereGreaterThan("population", 1000000)
 .whereLessThan("density", 10000);

Kotlin+KTX Android

val query = db.collection("cities")
 .whereGreaterThan("population", 1000000)
 .whereLessThan("density", 10000)

Go

   query := client.Collection("cities").
      Where("population", ">", 1000000).
      Where("density", "<", 10000)

Java

db.collection("cities")
  .whereGreaterThan("population", 1000000)
  .whereLessThan("density", 10000);

Node.js

db.collection("cities")
  .where('population', '>', 1000000),
  .where('density', '<', 10000)

Python

from google.cloud import firestore

db = firestore.Client()
query = db.collection("cities")
.where("population", ">", 1000000)
.where("density", "<", 10000)

PHP

$collection = $db->collection('samples/php/cities');
$chainedQuery = $collection
    ->where('population', '>', 1000000)
    ->where('density', '<', 10000);

C#

CollectionReference citiesRef = db.Collection("cities");
Query query = citiesRef
    .WhereGreaterThan("Population", 1000000)
    .WhereLessThan("Density", 10000);
QuerySnapshot querySnapshot = await query.GetSnapshotAsync();
foreach (DocumentSnapshot documentSnapshot in querySnapshot)
{
    var name = documentSnapshot.GetValue<string>("Name");
    var population = documentSnapshot.GetValue<int>("Population");
    var density = documentSnapshot.GetValue<int>("Density");
    Console.WriteLine($"City '{name}' returned by query. Population={population}; Density={density}");
}

Ruby

query = cities_ref.where("population", ">", "1000000")
                  .where("density", "<", 10000)

C++

CollectionReference cities_ref = db->Collection("cities");
Query query = cities_ref.WhereGreaterThan("population", FieldValue::Integer(1000000))
                       .WhereLessThan("density", FieldValue::Integer(10000));

Unity

CollectionReference citiesRef = db.Collection("cities");
Query query = citiesRef.WhereGreaterThan("population", 1000000)
                      .WhereLessThan("density", 10000);

Dart

final citiesRef = FirebaseFirestore.instance.collection('cities')
final query = citiesRef.where("population", isGreaterThan: 1000000)
                  .where("density", isLessThan: 10000);

建立索引的注意事項

執行查詢前,請先瞭解查詢和 Firestore 資料模型

在 Firestore 中,查詢的 ORDER BY 子句會決定可使用哪些索引來提供查詢。舉例來說,ORDER BY a ASC, b ASC 查詢需要在 a ASC, b ASC 欄位上建立複合式索引。

如要盡量提升 Firestore 查詢的效能和成本效益,請調整索引中的欄位順序。如要達成這個目標,請確保索引從左到右排序,讓查詢精簡為資料集,避免掃描不必要的索引項目。

假設您想搜尋員工集合,找出薪資超過 $100,000 美元且年資大於 0 的美國員工。根據您對資料集的瞭解,您知道薪資限制比經驗限制更具選擇性。理想的索引是 (salary [...], experience [...]),可減少索引掃描次數。因此,快速且符合成本效益的查詢會先排序 salary,再排序 experience,如下所示:

Java

db.collection("employees")
  .whereGreaterThan("salary", 100000)
  .whereGreaterThan("experience", 0)
  .orderBy("salary")
  .orderBy("experience");

Node.js

db.collection("employees")
  .where("salary", ">", 100000)
  .where("experience", ">", 0)
  .orderBy("salary")
  .orderBy("experience");

Python

db.collection("employees")
  .where("salary", ">", 100000)
  .where("experience", ">", 0)
  .order_by("salary")
  .order_by("experience");

最佳化索引的最佳做法

最佳化索引時,請注意下列最佳做法。

依等式排序索引欄位,然後依最具選擇性的範圍或不等式欄位排序

Firestore 會使用複合索引最左側的欄位,滿足 orderBy() 查詢第一個欄位的等式限制,以及範圍或不等式限制 (如有)。這些限制可減少 Firestore 掃描的索引項目數量。Firestore 會使用索引的其餘欄位,滿足查詢的其他範圍或不等式限制。這些限制不會減少 Firestore 掃描的索引項目數量,但會篩除不相符的文件,因此傳回給用戶端的檔案數量會減少。

如要進一步瞭解如何建立有效率的索引,請參閱索引屬性

依查詢限制選擇性遞減順序排列欄位

為確保 Firestore 為查詢選取最佳索引,請指定 orderBy() 子句,依查詢限制選擇性遞減順序排序欄位。如果選取性較高,系統會比對較小的文件子集;如果選取性較低,系統則會比對較大的文件子集。請務必在索引排序中,先選取選擇性較高的範圍或不相等欄位,再選取選擇性較低的欄位。

為盡量減少 Firestore 掃描及透過網路傳回的文件數量,您應一律依查詢限制選擇性的遞減順序排序欄位。如果結果集並非依所需順序排列,且結果集預計很小,您可以實作用戶端邏輯,按照預期順序重新排序。

舉例來說,假設您想搜尋員工集合,找出薪資超過 $100,000 美元的美國員工,並依員工年資排序結果。如果您預期只有少數員工的薪資會超過 $100,000 美元,最有效率的查詢撰寫方式如下:

Java

db.collection("employees")
  .whereGreaterThan("salary", 100000)
  .orderBy("salary")
  .get()
  .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
        @Override
        public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
          // Order results by `experience`
        }
    });;

Node.js

const querySnapshot = await db.collection('employees')
                              .where("salary", ">", 100000)
                              .orderBy("salary")
                              .get();

// Order results by `experience`

Python

results = db.collection("employees")
            .where("salary", ">", 100000)
            .order_by("salary")
            .stream()

// Order results by `experience`

在查詢中加入 experience 排序條件會產生相同的文件集,並避免在用戶端重新排序結果,但查詢讀取的索引項目可能比先前的查詢多出許多無關項目。這是因為 Firestore 一律會優先採用索引欄位前置字元與查詢的排序子句相符的索引。如果 experience 已加入 order by 子句,Firestore 會選取 (experience [...], salary [...]) 索引來計算查詢結果。由於 experience 沒有其他限制,Firestore 會先讀取 employees 集合的所有索引項目,再套用 salary 篩選器,找出最終結果集。也就是說,系統仍會讀取不符合 salary 篩選條件的索引項目,進而增加查詢的延遲時間和費用。

定價

如果查詢針對多個欄位使用範圍和不等式篩選條件,系統會根據讀取的檔案和索引項目計費。

詳情請參閱定價頁面。

限制

除了查詢限制外,使用含多個欄位範圍和不等式篩選條件的查詢時,請注意下列限制:

  • 系統不支援對文件欄位使用範圍或不等式篩選條件,且只對文件鍵 (__name__) 使用等式限制的查詢。
  • Firestore 將範圍或不相等欄位的數量限制為 10 個。這是為了避免查詢的執行費用過高。

後續步驟