Recuperar o documento do Firestore como um mapa
Mantenha tudo organizado com as coleções
Salve e categorize o conteúdo com base nas suas preferências.
Recuperar o documento do Firestore como um mapa
Mais informações
Para ver a documentação detalhada que inclui este exemplo de código, consulte:
Exemplo de código
Exceto em caso de indicação contrária, o conteúdo desta página é licenciado de acordo com a Licença de atribuição 4.0 do Creative Commons, e as amostras de código são licenciadas de acordo com a Licença Apache 2.0. Para mais detalhes, consulte as políticas do site do Google Developers. Java é uma marca registrada da Oracle e/ou afiliadas.
[[["Fácil de entender","easyToUnderstand","thumb-up"],["Meu problema foi resolvido","solvedMyProblem","thumb-up"],["Outro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Informações incorretas ou exemplo de código","incorrectInformationOrSampleCode","thumb-down"],["Não contém as informações/amostras de que eu preciso","missingTheInformationSamplesINeed","thumb-down"],["Problema na tradução","translationIssue","thumb-down"],["Outro","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)."]]