获取嵌套集合中的 Firestore 文档

使用集合让一切井井有条 根据您的偏好保存内容并对其进行分类。

获取嵌套集合中的 Firestore 文档

深入探索

如需查看包含此代码示例的详细文档,请参阅以下内容:

代码示例

C#

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

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)
}

Java

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

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

Node.js

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

PHP

$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

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

Ruby

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

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器