Esegui query su un gruppo di raccolte Firestore con un filtro eq
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Esegui query su un gruppo di raccolte Firestore con un filtro eq
Per saperne di più
Per la documentazione dettagliata che include questo esempio di codice, vedi quanto segue:
Esempio di codice
Salvo quando diversamente specificato, i contenuti di questa pagina sono concessi in base alla licenza Creative Commons Attribution 4.0, mentre gli esempi di codice sono concessi in base alla licenza Apache 2.0. Per ulteriori dettagli, consulta le norme del sito di Google Developers. Java è un marchio registrato di Oracle e/o delle sue consociate.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","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)."]]