排序

說明

根據一或多個指定的排序順序,將輸入文件排序。

範例

Web

const results = await execute(db.pipeline()
  .collection("books")
  .sort(
    field("release_date").descending(), field("author").ascending()
  )
);
Swift
let results = try await db.pipeline()
  .collection("books")
  .sort([
    Field("release_date").descending(), Field("author").ascending()
  ])
  .execute()

Kotlin

val results = db.pipeline()
    .collection("books")
    .sort(
        field("release_date").descending(),
        field("author").ascending()
    )
    .execute()

Java

Task<Pipeline.Snapshot> results = db.pipeline()
    .collection("books")
    .sort(
        field("release_date").descending(),
        field("author").ascending()
    )
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

results = (
    client.pipeline()
    .collection("books")
    .sort(Field.of("release_date").descending(), Field.of("author").ascending())
    .execute()
)
Java
Pipeline.Snapshot results =
    firestore
        .pipeline()
        .collection("books")
        .sort(descending(field("release_date")), ascending(field("author")))
        .execute()
        .get();

行為

排列順序

排序順序遵循 Cloud Firestore 的值類型順序

結果的確定性順序

如果查詢中沒有 sort 階段,傳回結果的順序非絕對,可能會因執行而異。如果存在 sort 階段,但排序運算式無法在傳回的結果中產生不重複的排序,傳回結果的排序仍可能因執行而異。

舉例來說,依城市名稱排序下列資料集中的城市:country

{ name: "Los Angeles", state: "CA", country: "USA", population: 3970000 },
{ name: "New York", state: "NY", country: "USA", population: 8530000 }
{ name: "San Francisco", state: "CA", country: "USA", population: 870000 },

可產生資料集中 3 份文件的任何排列組合,因為這些文件都具有相同的 country 屬性。如要產生結果的確定性排序,可以在 name 的 order: 中附加排序順序:

Node.js

const results = await db.pipeline()
  .collection("/cities")
  .sort(field("country").ascending(), fieldd("__name__").ascending())
  .execute();

如果多個文件具有相同的 country 值,系統會使用不重複的文件名稱做為排序依據。請注意,與 country 欄位共同構成集合中文件專屬鍵的任何其他欄位,都可用於產生結果的確定性順序。

排序同等值

系統會將同等值排序在一起,但同等類別中的結果順序並非根據「結果的確定性順序」討論內容而定。舉例來說,針對下列資料集,依 size 遞增排序城市:

{ name: "Los Angeles", state: "CA", country: "USA", size: 3970000 },
{ name: "Mexico City", state: null, country: "Mexico", size: 3970000.0 },

可產生資料集中 2 個文件的任何排列組合,因為這兩個文件都有等效的 size3970000

多個排序階段

如果查詢包含多個連續排序階段,只有最後一個排序階段會影響查詢結果。請注意,這與 Core API 中 orderBy 子句的行為不同。

前 N 項排序最佳化

sort 後使用 limit(...) 時,可能會使用前 N 名排序。這項最佳化措施可限制排序階段的記憶體用量,因為系統一次只會儲存 N 個文件 (如 limit(...) 所定義),因此排序作業的記憶體效率更高。

空值和缺席值

如果文件中沒有排序中指定的欄位,系統會將該欄位的值視為 null 進行排序。舉例來說,針對下列資料集,依城市名稱的state以遞增順序排序城市:

{ name: "Los Angeles", state: "CA", country: "USA", population: 3970000 },
{ name: "Mexico City", state: null, country: "Mexico", population: 9200000 },
{ name: "New York", state: "NY", country: "USA", population: 8530000 }
{ name: "San Francisco", state: "CA", country: "USA", population: 870000 },
{ name: "Toronto", country: "Canada", population: 2930000 },

產生以下結果,其中「Toronto」文件和「Mexico City」文件會排序為 null,並顯示在其他文件之前。

{ name: "Toronto", country: "Canada", population: 2930000 },
{ name: "Mexico City", state: null, country: "Mexico", population: 9200000 },
{ name: "Los Angeles", state: "CA", country: "USA", population: 3970000 },
{ name: "San Francisco", state: "CA", country: "USA", population: 870000 },
{ name: "New York", state: "NY", country: "USA", population: 8530000 }