並べ替え(変換ステージ)

説明

指定された 1 つ以上の並べ替え順に基づいて、入力ドキュメントを並べ替えます。

例

ウェブ

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
Android
val results = db.pipeline()
    .collection("books")
    .sort(
        field("release_date").descending(),
        field("author").ascending()
    )
    .execute()
Java
Android
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)

行動

並べ替え順

並べ替え順は 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 },

すべてのドキュメントに同じ country 属性があるため、データセット内の 3 つのドキュメントの順列を生成できます。結果の決定的順序を生成するには、 並べ替え順を 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 },

両方の ドキュメントに同じsize値3970000があるため、データセット内の 2 つのドキュメントの順列を生成できます。

複数の並べ替えステージ

クエリに複数の連続する並べ替えステージが含まれている場合、クエリ結果に影響するのは最後の並べ替えステージのみです。これは、Core API の orderBy 句の動作とは異なることに注意してください。

上位 N 個の並べ替えの最適化

sort の後に limit(...) を使用すると、上位 N 個の並べ替え が使用されることがあります。この最適化により、並べ替えステージのメモリ使用量が制限されます。並べ替えステージでは、 limit(...) で定義されているように、一度に N 個のドキュメントのみを保存できるため、並べ替えのメモリ効率が向上します。

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 }