ネストされたコレクション内の Firestore ドキュメントを取得する

ネストされたコレクション内の Firestore ドキュメントを取得する

もっと見る

このコードサンプルを含む詳細なドキュメントについては、以下をご覧ください。

コードサンプル

C#

Firestore に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。

DocumentReference cityRef = db.Collection("cities").Document("SF");
IAsyncEnumerable<CollectionReference> subcollections = cityRef.ListCollectionsAsync();
IAsyncEnumerator<CollectionReference> subcollectionsEnumerator = subcollections.GetAsyncEnumerator(default);
while (await subcollectionsEnumerator.MoveNextAsync())
{
    CollectionReference subcollectionRef = subcollectionsEnumerator.Current;
    Console.WriteLine("Found subcollection with ID: {0}", subcollectionRef.Id);
}

Go

Firestore に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。


import (
	"context"
	"fmt"

	"cloud.google.com/go/firestore"
	"google.golang.org/api/iterator"
)

func getCollections(ctx context.Context, client *firestore.Client) error {
	iter := client.Collection("cities").Doc("SF").Collections(ctx)
	for {
		collRef, err := iter.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return err
		}
		fmt.Printf("Found collection with id: %s\n", collRef.ID)
	}
	return nil
}

Java

Firestore に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。

Iterable<CollectionReference> collections =
    db.collection("cities").document("SF").listCollections();

for (CollectionReference collRef : collections) {
  System.out.println("Found subcollection with id: " + collRef.getId());
}

Node.js

Firestore に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。

const sfRef = db.collection('cities').doc('SF');
const collections = await sfRef.listCollections();
collections.forEach(collection => {
  console.log('Found subcollection with id:', collection.id);
});

PHP

Firestore に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。

$cityRef = $db->collection('samples/php/cities')->document('SF');
$collections = $cityRef->collections();
foreach ($collections as $collection) {
    printf('Found subcollection with id: %s' . PHP_EOL, $collection->id());
}

Python

Firestore に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。

collections = db.collection("cities").document("SF").collections()
for collection in collections:
    for doc in collection.stream():
        print(f"{doc.id} => {doc.to_dict()}")

Ruby

Firestore に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。

city_ref = firestore.doc "#{collection_path}/SF"
city_ref.cols do |col|
  puts col.collection_id
end

次のステップ

他の Google Cloud プロダクトに関連するコードサンプルの検索およびフィルタ検索を行うには、Google Cloud のサンプルをご覧ください。