コレクション

説明

指定したコレクションからすべてのドキュメントを返します。コレクションはネストできます。

構文

Node.js

const results = await db.pipeline()
  .collection('/cities/SF/departments')
  .execute();

クライアントの例

Web

const results = await execute(db.pipeline()
  .collection("users/bob/games")
  .sort(field("name").ascending())
  );
Swift
let results = try await db.pipeline()
  .collection("users/bob/games")
  .sort([Field("name").ascending()])
  .execute()

Kotlin

val results = db.pipeline()
    .collection("users/bob/games")
    .sort(field("name").ascending())
    .execute()

Java

Task<Pipeline.Snapshot> results = db.pipeline()
    .collection("users/bob/games")
    .sort(field("name").ascending())
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

results = (
    client.pipeline()
    .collection("users/bob/games")
    .sort(Field.of("name").ascending())
    .execute()
)
Java
Pipeline.Snapshot results =
    firestore
        .pipeline()
        .collection("users/bob/games")
        .sort(ascending(field("name")))
        .execute()
        .get();

動作

collection ステージを使用するには、パイプラインの最初のステージとして登場する必要があります。

collection ステージから返されるドキュメントの順序は不安定であり、それに依存すべきではありません。後続の並べ替えステージを使用して、決定的な順序を取得できます。

ドキュメントの例:

Node.js

await db.collection('cities').doc('SF').set({name: 'San Francsico', state: 'California'});
await db.collection('cities').doc('NYC').set({name: 'New York City', state: 'New York'});
await db.collection('cities').doc('CHI').set({name: 'Chicago', state: 'Illinois'});
await db.collection('states').doc('CA').set({name: 'California'});

collection ステージを使用して、cities コレクション内のすべての都市を取得し、名前の昇順で並べ替えることができます。

Node.js

const results = await db.pipeline()
  .collection('/cities')
  .sort(field('name').ascending())
  .execute();

このクエリは次のドキュメントを生成します。

  {name: 'Chicago', state: 'Illinois'}
  {name: 'New York City', state: 'New York'}
  {name: 'San Francisco', state: 'California'}

サブコレクション

collection ステージを使用してステージのフルパスを指定することで、特定の親の下にあるコレクションをターゲットにすることもできます。

ドキュメントの例:

Node.js

await db.collection('cities/SF/departments').doc('building').set({name: 'SF Building Deparment', employees: 750});
await db.collection('cities/NY/departments').doc('building').set({name: 'NY Building Deparment', employees: 1000});
await db.collection('cities/CHI/departments').doc('building').set({name: 'CHI Building Deparment', employees: 900});
await db.collection('cities/NY/departments').doc('finance').set({name: 'NY Finance Deparment', employees: 1200});

この例では、ニューヨーク市の部門のみを取得します。

Node.js

const results = await db.pipeline()
  .collection('/cities/NY/departments')
  .sort(field('employees').ascending())
  .execute();

これにより、フルパス cities/NY/departments の下にあるすべての部門が返されます。

  {name: 'NY Building Deparment', employees: 1000}
  {name: 'NY Finance Deparment', employees: 1200}