Recupera documento Firestore come mappa
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Recupera documento Firestore come mappa
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"]],[],[[["\u003cp\u003eThis content demonstrates how to retrieve a Firestore document and access its data as a map or dictionary in various programming languages.\u003c/p\u003e\n"],["\u003cp\u003eThe examples utilize a document with the ID "SF" from the "cities" collection as a demonstration.\u003c/p\u003e\n"],["\u003cp\u003eEach code sample checks if the document exists before attempting to access its data.\u003c/p\u003e\n"],["\u003cp\u003eThe content specifies that setting up Application Default Credentials is required to authenticate with Firestore.\u003c/p\u003e\n"],["\u003cp\u003eThe content suggests the user check out the Google Cloud Sample browser to search for code samples for other Google Cloud Products.\u003c/p\u003e\n"]]],[],null,["# Retrieve Firestore Document as Map\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 docRef = db.Collection(\"cities\").Document(\"SF\");\n DocumentSnapshot snapshot = await docRef.GetSnapshotAsync();\n if (snapshot.Exists)\n {\n Console.WriteLine(\"Document data for {0} document:\", snapshot.Id);\n Dictionary\u003cstring, object\u003e city = snapshot.ToDictionary();\n foreach (KeyValuePair\u003cstring, object\u003e pair in city)\n {\n Console.WriteLine(\"{0}: {1}\", pair.Key, pair.Value);\n }\n }\n else\n {\n Console.WriteLine(\"Document {0} does not exist!\", snapshot.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 )\n\n func docAsMap(ctx context.Context, client *firestore.Client) (map[string]interface{}, error) {\n \tdsnap, err := client.Collection(\"cities\").Doc(\"SF\").Get(ctx)\n \tif err != nil {\n \t\treturn nil, err\n \t}\n \tm := dsnap.https://cloud.google.com/go/docs/reference/cloud.google.com/go/firestore/latest/index.html#cloud_google_com_go_firestore_DocumentSnapshot_Data()\n \tfmt.Printf(\"Document data: %#v\\n\", m)\n \treturn m, 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 DocumentReference docRef = db.collection(\"cities\").document(\"SF\");\n // asynchronously retrieve the document\n ApiFuture\u003cDocumentSnapshot\u003e future = docRef.get();\n // ...\n // future.get() blocks on response\n DocumentSnapshot document = future.get();\n if (document.exists()) {\n System.out.println(\"Document data: \" + document.getData());\n } else {\n System.out.println(\"No such document!\");\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 cityRef = db.collection('cities').doc('SF');\n const doc = await cityRef.get();\n if (!doc.exists) {\n console.log('No such document!');\n } else {\n console.log('Document data:', 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 $docRef = $db-\u003ecollection('samples/php/cities')-\u003edocument('SF');\n $snapshot = $docRef-\u003esnapshot();\n\n if ($snapshot-\u003eexists()) {\n printf('Document data:' . PHP_EOL);\n print_r($snapshot-\u003edata());\n } else {\n printf('Document %s does not exist!' . PHP_EOL, $snapshot-\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 doc_ref = db.collection(\"cities\").document(\"SF\")\n\n doc = doc_ref.get()\n if doc.exists:\n print(f\"Document data: {doc.to_dict()}\")\n else:\n print(\"No such document!\")\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 doc_ref = firestore.doc \"#{collection_path}/SF\"\n snapshot = doc_ref.get\n if snapshot.exists?\n puts \"#{snapshot.document_id} data: #{snapshot.data}.\"\n else\n puts \"Document #{snapshot.document_id} does not exist!\"\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)."]]