使用巢狀地圖新增 Firestore 文件
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
使用巢狀地圖新增 Firestore 文件
深入探索
如需包含這個程式碼範例的詳細說明文件,請參閱下列內容:
程式碼範例
Java
如要向 Firestore 進行驗證,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
Node.js
如要向 Firestore 進行驗證,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
PHP
如要向 Firestore 進行驗證,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
Python
如要向 Firestore 進行驗證,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
Ruby
如要向 Firestore 進行驗證,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 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 page demonstrates how to add a Firestore document containing various data types, including strings, booleans, numbers, null values, arrays, and nested objects.\u003c/p\u003e\n"],["\u003cp\u003eThe code examples illustrate how to structure data with these diverse types in a document, using a nested map (object) for organized data.\u003c/p\u003e\n"],["\u003cp\u003eAuthentication to Firestore requires setting up Application Default Credentials, as detailed in the linked documentation.\u003c/p\u003e\n"],["\u003cp\u003eCode samples are provided in multiple programming languages: C#, Go, Java, Node.js, PHP, Python, and Ruby.\u003c/p\u003e\n"],["\u003cp\u003eThe example shows how to create and set a document with the reference document name "one" within the "data" collection.\u003c/p\u003e\n"]]],[],null,["# Add a Firestore document using a nested map\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Add and update data](/firestore/native/docs/manage-data/add-data)\n- [Add data to Cloud Firestore](https://firebase.google.com/docs/firestore/manage-data/add-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(\"data\").Document(\"one\");\n Dictionary\u003cstring, object\u003e docData = new Dictionary\u003cstring, object\u003e\n {\n { \"stringExample\", \"Hello World\" },\n { \"booleanExample\", false },\n { \"numberExample\", 3.14159265 },\n { \"nullExample\", null },\n { \"arrayExample\", new object[] { 5, true, \"Hello\" } },\n { \"objectExample\", new Dictionary\u003cstring, object\u003e\n {\n { \"a\", 5 },\n { \"b\", true},\n }\n }\n };\n\n await docRef.SetAsync(docData);\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\"log\"\n \t\"time\"\n\n \t\"cloud.google.com/go/firestore\"\n )\n\n func addDocDataTypes(ctx context.Context, client *firestore.Client) error {\n \tdoc := make(map[string]interface{})\n \tdoc[\"stringExample\"] = \"Hello world!\"\n \tdoc[\"booleanExample\"] = true\n \tdoc[\"numberExample\"] = 3.14159265\n \tdoc[\"dateExample\"] = time.Now()\n \tdoc[\"arrayExample\"] = []interface{}{5, true, \"hello\"}\n \tdoc[\"nullExample\"] = nil\n \tdoc[\"objectExample\"] = map[string]interface{}{\n \t\t\"a\": 5,\n \t\t\"b\": true,\n \t}\n\n \t_, err := client.Collection(\"data\").Doc(\"one\").Set(ctx, doc)\n \tif err != nil {\n \t\t// Handle any errors in an appropriate way, such as returning them.\n \t\tlog.Printf(\"An error has occurred: %s\", err)\n \t}\n\n \treturn err\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 Map\u003cString, Object\u003e docData = new HashMap\u003c\u003e();\n docData.put(\"stringExample\", \"Hello, World\");\n docData.put(\"booleanExample\", false);\n docData.put(\"numberExample\", 3.14159265);\n docData.put(\"nullExample\", null);\n\n ArrayList\u003cObject\u003e arrayExample = new ArrayList\u003c\u003e();\n Collections.addAll(arrayExample, 5L, true, \"hello\");\n docData.put(\"arrayExample\", arrayExample);\n\n Map\u003cString, Object\u003e objectExample = new HashMap\u003c\u003e();\n objectExample.put(\"a\", 5L);\n objectExample.put(\"b\", true);\n\n docData.put(\"objectExample\", objectExample);\n\n ApiFuture\u003cWriteResult\u003e future = db.collection(\"data\").document(\"one\").set(docData);\n System.out.println(\"Update time : \" + future.get().getUpdateTime());\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 data = {\n stringExample: 'Hello, World!',\n booleanExample: true,\n numberExample: 3.14159265,\n dateExample: Timestamp.fromDate(new Date('December 10, 1815')),\n arrayExample: [5, true, 'hello'],\n nullExample: null,\n objectExample: {\n a: 5,\n b: true\n }\n };\n\n const res = await db.collection('data').doc('one').set(data);\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 $data = [\n 'stringExample' =\u003e 'Hello World',\n 'booleanExample' =\u003e true,\n 'numberExample' =\u003e 3.14159265,\n 'dateExample' =\u003e new Timestamp(new DateTime()),\n 'arrayExample' =\u003e array(5, true, 'hello'),\n 'nullExample' =\u003e null,\n 'objectExample' =\u003e ['a' =\u003e 5, 'b' =\u003e true],\n 'documentReferenceExample' =\u003e $db-\u003ecollection('samples/php/data')-\u003edocument('two'),\n ];\n $db-\u003ecollection('samples/php/data')-\u003edocument('one')-\u003eset($data);\n printf('Set multiple data-type data for the one document in the data collection.' . PHP_EOL);\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 data = {\n \"stringExample\": \"Hello, World!\",\n \"booleanExample\": True,\n \"numberExample\": 3.14159265,\n \"dateExample\": datetime.datetime.now(tz=datetime.timezone.utc),\n \"arrayExample\": [5, True, \"hello\"],\n \"nullExample\": None,\n \"objectExample\": {\"a\": 5, \"b\": True},\n }\n\n db.collection(\"data\").document(\"one\").set(data)\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}/one\"\n\n data = {\n stringExample: \"Hello, World!\",\n booleanExample: true,\n numberExample: 3.14159265,\n dateExample: DateTime.now,\n arrayExample: [5, true, \"hello\"],\n nullExample: nil,\n objectExample: {\n a: 5,\n b: true\n }\n }\n\n doc_ref.set data\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)."]]