説明
指定された 1 つ以上の並べ替え順に基づいて、入力ドキュメントを並べ替えます。
構文
次の例では、cities コレクションを population で昇順に並べ替えます。
Node.js
const results = await db.pipeline()
.collection("/cities")
.sort(Field.of("population").ascending())
.execute();
次の例では、cities コレクションを都市名の length で昇順に並べ替えます。
Node.js
const results = await db.pipeline()
.collection("/cities")
.sort(Field.of("name").charLength().ascending())
.execute();
クライアントの例
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 の並べ替え順を順序に追加します。
Node.js
const results = await db.pipeline()
.collection("/cities")
.sort(Field.of("country").ascending(), Field.of("__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 件の並べ替えが使用されることがあります。この最適化では、一度に保存できるドキュメントの数を 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}