說明
根據一或多個指定的排列順序,將輸入文件排序。
範例
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();
Go
snapshot := client.Pipeline(). Collection("books"). Sort(firestore.Orders( firestore.Descending(firestore.FieldOf("release_date")), firestore.Ascending(firestore.FieldOf("author")), )). Execute(ctx)
行為
排列順序
排列順序遵循 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 上附加排序順序:
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 份文件的任何排列組合,因為這兩份文件具有相同的 size 值 3970000。
多個排序階段
如果查詢包含多個連續排序階段,只有最後一個排序階段會影響查詢結果。請注意,這與 Core API 中 orderBy 子句的行為不同。
前 N 項排序最佳化
在 sort 後使用 limit(...) 時,可能會使用前 N 項排序。這項最佳化措施會限制排序階段的記憶體用量,只允許一次儲存 N 份文件 (由 limit(...) 定義),讓排序更有效率地使用記憶體。
Null 和 Absent 值
如果排序中指定的欄位不存在於文件中,系統會將其值排序為 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 }