Get Firestore documents in nested collections
Stay organized with collections
Save and categorize content based on your preferences.
Get Firestore documents in nested collections
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis content demonstrates how to retrieve and list subcollections within a Firestore document, using the "cities" collection as an example.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples are provided in various languages, including C#, Go, Java, Node.js, PHP, Python, and Ruby, allowing developers to use the language they are most familiar with.\u003c/p\u003e\n"],["\u003cp\u003eEach code sample includes instructions to set up Application Default Credentials for Firestore authentication, pointing to a resource for setting up local development.\u003c/p\u003e\n"],["\u003cp\u003eThe document also provides links to the official Firebase documentation for further reading on retrieving data in Firestore.\u003c/p\u003e\n"]]],[],null,["# Get Firestore documents in nested collections\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Get data with Cloud Firestore](https://firebase.google.com/docs/firestore/query-data/get-data)\n- [Getting data](/firestore/native/docs/query-data/get-data)\n\nCode sample\n-----------\n\n### C#\n\n\nTo authenticate to Firestore, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n DocumentReference cityRef = db.Collection(\"cities\").Document(\"SF\");\n IAsyncEnumerable\u003cCollectionReference\u003e subcollections = cityRef.ListCollectionsAsync();\n IAsyncEnumerator\u003cCollectionReference\u003e subcollectionsEnumerator = subcollections.GetAsyncEnumerator(default);\n while (await subcollectionsEnumerator.MoveNextAsync())\n {\n CollectionReference subcollectionRef = subcollectionsEnumerator.Current;\n Console.WriteLine(\"Found subcollection with ID: {0}\", subcollectionRef.Id);\n }\n\n### Go\n\n\nTo authenticate to Firestore, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n import (\n \t\"context\"\n \t\"fmt\"\n\n \t\"cloud.google.com/go/firestore\"\n \t\"google.golang.org/api/iterator\"\n )\n\n func getCollections(ctx context.Context, client *firestore.Client) error {\n \titer := client.Collection(\"cities\").Doc(\"SF\").Collections(ctx)\n \tfor {\n \t\tcollRef, err := iter.Next()\n \t\tif err == iterator.Done {\n \t\t\tbreak\n \t\t}\n \t\tif err != nil {\n \t\t\treturn err\n \t\t}\n \t\tfmt.Printf(\"Found collection with id: %s\\n\", collRef.ID)\n \t}\n \treturn nil\n }\n\n### Java\n\n\nTo authenticate to Firestore, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n Iterable\u003cCollectionReference\u003e collections =\n db.collection(\"cities\").document(\"SF\").listCollections();\n\n for (CollectionReference collRef : collections) {\n System.out.println(\"Found subcollection with id: \" + collRef.getId());\n }\n\n### Node.js\n\n\nTo authenticate to Firestore, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n const sfRef = db.collection('cities').doc('SF');\n const collections = await sfRef.listCollections();\n collections.forEach(collection =\u003e {\n console.log('Found subcollection with id:', collection.id);\n });\n\n### PHP\n\n\nTo authenticate to Firestore, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n $cityRef = $db-\u003ecollection('samples/php/cities')-\u003edocument('SF');\n $collections = $cityRef-\u003ecollections();\n foreach ($collections as $collection) {\n printf('Found subcollection with id: %s' . PHP_EOL, $collection-\u003eid());\n }\n\n### Python\n\n\nTo authenticate to Firestore, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n city_ref = db.collection(\"cities\").document(\"SF\")\n collections = city_ref.collections()\n for collection in collections:\n for doc in collection.stream():\n print(f\"{doc.id} =\u003e {doc.to_dict()}\")\n\n### Ruby\n\n\nTo authenticate to Firestore, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n city_ref = firestore.doc \"#{collection_path}/SF\"\n city_ref.cols do |col|\n puts col.collection_id\n end\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=firestore)."]]