Add a Firestore document using a nested map
Stay organized with collections
Save and categorize content based on your preferences.
Add a Firestore document using a nested map
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","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)."]]