Firestore ドキュメントをマップとして取得する
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Firestore ドキュメントをマップとして取得する
もっと見る
このコードサンプルを含む詳細なドキュメントについては、以下をご覧ください。
コードサンプル
C#
Firestore に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。
Go
Firestore に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。
Java
Firestore に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。
Node.js
Firestore に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。
PHP
Firestore に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。
Python
Firestore に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。
Ruby
Firestore に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["わかりにくい","hardToUnderstand","thumb-down"],["情報またはサンプルコードが不正確","incorrectInformationOrSampleCode","thumb-down"],["必要な情報 / サンプルがない","missingTheInformationSamplesINeed","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["その他","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)."]]