Menambahkan dokumen Firestore menggunakan peta bertingkat
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Menambahkan dokumen Firestore menggunakan peta bertingkat
Mempelajari lebih lanjut
Untuk dokumentasi mendetail yang menyertakan contoh kode ini, lihat artikel berikut:
Contoh kode
Kecuali dinyatakan lain, konten di halaman ini dilisensikan berdasarkan Lisensi Creative Commons Attribution 4.0, sedangkan contoh kode dilisensikan berdasarkan Lisensi Apache 2.0. Untuk mengetahui informasi selengkapnya, lihat Kebijakan Situs Google Developers. Java adalah merek dagang terdaftar dari Oracle dan/atau afiliasinya.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","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)."]]