Get Firestore Documents created from custom classes
Stay organized with collections
Save and categorize content based on your preferences.
Get Firestore Documents created from custom classes
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 create Firestore documents using code examples in various languages, including C#, Go, Java, Node.js, PHP, Python, and Ruby.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples provided illustrate how to add city data to a Firestore collection, including details like name, state, country, capital status, and population.\u003c/p\u003e\n"],["\u003cp\u003eEach language's example shows how to set up authentication to Firestore using Application Default Credentials.\u003c/p\u003e\n"],["\u003cp\u003eThe data that is added covers 5 cities, these cities are San Francisco, Los Angeles, Washington D.C, Tokyo, and Beijing.\u003c/p\u003e\n"],["\u003cp\u003eIt is possible to access more code samples through the google cloud sample browser.\u003c/p\u003e\n"]]],[],null,["# Get Firestore Documents created from custom classes\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- [Perform simple and compound queries in Cloud Firestore](https://firebase.google.com/docs/firestore/query-data/queries)\n- [Query and filter data](/firestore/native/docs/query-data/queries)\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 CollectionReference citiesRef = db.Collection(\"cities\");\n await citiesRef.Document(\"SF\").SetAsync(new Dictionary\u003cstring, object\u003e(){\n { \"Name\", \"San Francisco\" },\n { \"State\", \"CA\" },\n { \"Country\", \"USA\" },\n { \"Capital\", false },\n { \"Population\", 860000 }\n });\n await citiesRef.Document(\"LA\").SetAsync(new Dictionary\u003cstring, object\u003e(){\n { \"Name\", \"Los Angeles\" },\n { \"State\", \"CA\" },\n { \"Country\", \"USA\" },\n { \"Capital\", false },\n { \"Population\", 3900000 }\n });\n await citiesRef.Document(\"DC\").SetAsync(new Dictionary\u003cstring, object\u003e(){\n { \"Name\", \"Washington D.C.\" },\n { \"State\", null },\n { \"Country\", \"USA\" },\n { \"Capital\", true },\n { \"Population\", 680000 }\n });\n await citiesRef.Document(\"TOK\").SetAsync(new Dictionary\u003cstring, object\u003e(){\n { \"Name\", \"Tokyo\" },\n { \"State\", null },\n { \"Country\", \"Japan\" },\n { \"Capital\", true },\n { \"Population\", 9000000 }\n });\n await citiesRef.Document(\"BJ\").SetAsync(new Dictionary\u003cstring, object\u003e(){\n { \"Name\", \"Beijing\" },\n { \"State\", null },\n { \"Country\", \"China\" },\n { \"Capital\", true },\n { \"Population\", 21500000 }\n });\n Console.WriteLine(\"Added example cities data to the cities collection.\");\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\n \t\"cloud.google.com/go/firestore\"\n )\n\n func prepareRetrieve(ctx context.Context, client *firestore.Client) error {\n \tcities := []struct {\n \t\tid string\n \t\tc City\n \t}{\n \t\t{id: \"SF\", c: City{Name: \"San Francisco\", State: \"CA\", Country: \"USA\", Capital: false, Population: 860000}},\n \t\t{id: \"LA\", c: City{Name: \"Los Angeles\", State: \"CA\", Country: \"USA\", Capital: false, Population: 3900000}},\n \t\t{id: \"DC\", c: City{Name: \"Washington D.C.\", Country: \"USA\", Capital: true, Population: 680000}},\n \t\t{id: \"TOK\", c: City{Name: \"Tokyo\", Country: \"Japan\", Capital: true, Population: 9000000}},\n \t\t{id: \"BJ\", c: City{Name: \"Beijing\", Country: \"China\", Capital: true, Population: 21500000}},\n \t}\n \tfor _, c := range cities {\n \t\t_, err := client.Collection(\"cities\").Doc(c.id).Set(ctx, c.c)\n \t\tif err != nil {\n \t\t\treturn err\n \t\t}\n \t}\n \treturn 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 CollectionReference cities = db.collection(\"cities\");\n List\u003cApiFuture\u003cWriteResult\u003e\u003e futures = new ArrayList\u003c\u003e();\n futures.add(\n cities\n .document(\"SF\")\n .set(\n new City(\n \"San Francisco\",\n \"CA\",\n \"USA\",\n false,\n 860000L,\n Arrays.asList(\"west_coast\", \"norcal\"))));\n futures.add(\n cities\n .document(\"LA\")\n .set(\n new City(\n \"Los Angeles\",\n \"CA\",\n \"USA\",\n false,\n 3900000L,\n Arrays.asList(\"west_coast\", \"socal\"))));\n futures.add(\n cities\n .document(\"DC\")\n .set(\n new City(\n \"Washington D.C.\", null, \"USA\", true, 680000L, Arrays.asList(\"east_coast\"))));\n futures.add(\n cities\n .document(\"TOK\")\n .set(\n new City(\n \"Tokyo\", null, \"Japan\", true, 9000000L, Arrays.asList(\"kanto\", \"honshu\"))));\n futures.add(\n cities\n .document(\"BJ\")\n .set(\n new City(\n \"Beijing\",\n null,\n \"China\",\n true,\n 21500000L,\n Arrays.asList(\"jingjinji\", \"hebei\"))));\n // (optional) block on operation\n ApiFutures.allAsList(futures).get();\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 citiesRef = db.collection('cities');\n\n await citiesRef.doc('SF').set({\n name: 'San Francisco', state: 'CA', country: 'USA',\n capital: false, population: 860000\n });\n await citiesRef.doc('LA').set({\n name: 'Los Angeles', state: 'CA', country: 'USA',\n capital: false, population: 3900000\n });\n await citiesRef.doc('DC').set({\n name: 'Washington, D.C.', state: null, country: 'USA',\n capital: true, population: 680000\n });\n await citiesRef.doc('TOK').set({\n name: 'Tokyo', state: null, country: 'Japan',\n capital: true, population: 9000000\n });\n await citiesRef.doc('BJ').set({\n name: 'Beijing', state: null, country: 'China',\n capital: true, population: 21500000\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 $citiesRef = $db-\u003ecollection('samples/php/cities');\n $citiesRef-\u003edocument('SF')-\u003eset([\n 'name' =\u003e 'San Francisco',\n 'state' =\u003e 'CA',\n 'country' =\u003e 'USA',\n 'capital' =\u003e false,\n 'population' =\u003e 860000,\n 'density' =\u003e 18000,\n ]);\n $citiesRef-\u003edocument('LA')-\u003eset([\n 'name' =\u003e 'Los Angeles',\n 'state' =\u003e 'CA',\n 'country' =\u003e 'USA',\n 'capital' =\u003e false,\n 'population' =\u003e 3900000,\n 'density' =\u003e 8000,\n ]);\n $citiesRef-\u003edocument('DC')-\u003eset([\n 'name' =\u003e 'Washington D.C.',\n 'state' =\u003e null,\n 'country' =\u003e 'USA',\n 'capital' =\u003e true,\n 'population' =\u003e 680000,\n 'density' =\u003e 11000,\n ]);\n $citiesRef-\u003edocument('TOK')-\u003eset([\n 'name' =\u003e 'Tokyo',\n 'state' =\u003e null,\n 'country' =\u003e 'Japan',\n 'capital' =\u003e true,\n 'population' =\u003e 9000000,\n 'density' =\u003e 16000,\n\n ]);\n $citiesRef-\u003edocument('BJ')-\u003eset([\n 'name' =\u003e 'Beijing',\n 'state' =\u003e null,\n 'country' =\u003e 'China',\n 'capital' =\u003e true,\n 'population' =\u003e 21500000,\n 'density' =\u003e 3500,\n ]);\n printf('Added example cities data to the cities 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 cities_ref = db.collection(\"cities\")\n cities_ref.document(\"BJ\").set(\n City(\"Beijing\", None, \"China\", True, 21500000, [\"hebei\"]).to_dict()\n )\n cities_ref.document(\"SF\").set(\n City(\n \"San Francisco\", \"CA\", \"USA\", False, 860000, [\"west_coast\", \"norcal\"]\n ).to_dict()\n )\n cities_ref.document(\"LA\").set(\n City(\n \"Los Angeles\", \"CA\", \"USA\", False, 3900000, [\"west_coast\", \"socal\"]\n ).to_dict()\n )\n cities_ref.document(\"DC\").set(\n City(\"Washington D.C.\", None, \"USA\", True, 680000, [\"east_coast\"]).to_dict()\n )\n cities_ref.document(\"TOK\").set(\n City(\"Tokyo\", None, \"Japan\", True, 9000000, [\"kanto\", \"honshu\"]).to_dict()\n )\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 cities_ref = firestore.col collection_path\n cities_ref.doc(\"SF\").set(\n {\n name: \"San Francisco\",\n state: \"CA\",\n country: \"USA\",\n capital: false,\n population: 860_000\n }\n )\n cities_ref.doc(\"LA\").set(\n {\n name: \"Los Angeles\",\n state: \"CA\",\n country: \"USA\",\n capital: false,\n population: 3_900_000\n }\n )\n cities_ref.doc(\"DC\").set(\n {\n name: \"Washington D.C.\",\n state: nil,\n country: \"USA\",\n capital: true,\n population: 680_000\n }\n )\n cities_ref.doc(\"TOK\").set(\n {\n name: \"Tokyo\",\n state: nil,\n country: \"Japan\",\n capital: true,\n population: 9_000_000\n }\n )\n cities_ref.doc(\"BJ\").set(\n {\n name: \"Beijing\",\n state: nil,\n country: \"China\",\n capital: true,\n population: 21_500_000\n }\n )\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)."]]