Query a Firestore collection group with an eq filter
Stay organized with collections
Save and categorize content based on your preferences.
Query a Firestore collection group with an eq filter
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"]],[],[],[],null,["# Query a Firestore collection group with an eq filter\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Perform simple and compound queries in Cloud Firestore](https://firebase.google.com/docs/firestore/query-data/queries)\n- [Query and filter data](/firestore/native/docs/query-data/queries)\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 Query museums = db.CollectionGroup(\"landmarks\").WhereEqualTo(\"Type\", \"museum\");\n QuerySnapshot querySnapshot = await museums.GetSnapshotAsync();\n foreach (DocumentSnapshot document in querySnapshot.Documents)\n {\n Console.WriteLine($\"{document.Reference.Path}: {document.GetValue\u003cstring\u003e(\"Name\")}\");\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 import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n\n \t\"cloud.google.com/go/firestore\"\n \t\"google.golang.org/api/iterator\"\n )\n\n // collectionGroupQuery runs a collection group query over the data created by\n // collectionGroupSetup.\n func collectionGroupQuery(w io.Writer, projectID string) error {\n \tctx := context.Background()\n\n \tclient, err := firestore.NewClient(ctx, projectID)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"firestore.NewClient: %w\", err)\n \t}\n \tdefer client.Close()\n\n \tit := client.https://cloud.google.com/go/docs/reference/cloud.google.com/go/firestore/latest/index.html#cloud_google_com_go_firestore_Client_CollectionGroup(\"landmarks\").https://cloud.google.com/go/docs/reference/cloud.google.com/go/firestore/latest/index.html#cloud_google_com_go_firestore_Query_Where(\"type\", \"==\", \"museum\").Documents(ctx)\n \tfor {\n \t\tdoc, err := it.Next()\n \t\tif err == iterator.Done {\n \t\t\tbreak\n \t\t}\n \t\tif err != nil {\n \t\t\treturn fmt.Errorf(\"documents iterator: %w\", err)\n \t\t}\n \t\tfmt.Fprintf(w, \"%s: %s\", doc.Ref.ID, doc.https://cloud.google.com/go/docs/reference/cloud.google.com/go/firestore/latest/index.html#cloud_google_com_go_firestore_DocumentSnapshot_Data()[\"name\"])\n \t}\n\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 final Query museums = db.collectionGroup(\"landmarks\").whereEqualTo(\"type\", \"museum\");\n final ApiFuture\u003cQuerySnapshot\u003e querySnapshot = museums.get();\n for (DocumentSnapshot document : querySnapshot.get().getDocuments()) {\n System.out.println(document.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 querySnapshot = await db.collectionGroup('landmarks').where('type', '==', 'museum').get();\n querySnapshot.forEach((doc) =\u003e {\n console.log(doc.id, ' =\u003e ', doc.data());\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 $museums = $db-\u003ecollectionGroup('landmarks')-\u003ewhere('type', '==', 'museum');\n foreach ($museums-\u003edocuments() as $document) {\n printf('%s =\u003e %s' . PHP_EOL, $document-\u003eid(), $document-\u003edata()['name']);\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 museums = db.collection_group(\"landmarks\").where(\n filter=FieldFilter(\"type\", \"==\", \"museum\")\n )\n docs = museums.stream()\n for doc in docs:\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 museums = firestore.collection_group(\"landmarks\").where(\"type\", \"==\", \"museum\")\n museums.get do |museum|\n puts \"#{museum[:type]} name is #{museum[:name]}.\"\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)."]]