Mendapatkan dokumen Firestore menggunakan jenis kustom
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Mendapatkan dokumen Firestore menggunakan jenis kustom.
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 content demonstrates how to retrieve a Firestore document using custom types in various programming languages like C#, Go, Java, PHP, and Python.\u003c/p\u003e\n"],["\u003cp\u003eEach code sample showcases the process of fetching a document from the "cities" collection, using "BJ" as the document ID, and converting it into a custom "City" object.\u003c/p\u003e\n"],["\u003cp\u003eThe examples highlight how to verify if a document exists before attempting to access its data, and then displaying information contained in said data if it does exist.\u003c/p\u003e\n"],["\u003cp\u003eAuthentication to Firestore is required and can be achieved by setting up Application Default Credentials, as indicated in the sample code.\u003c/p\u003e\n"],["\u003cp\u003eFurther code samples related to other Google Cloud products can be found using the Google Cloud sample browser, linked in the documentation.\u003c/p\u003e\n"]]],[],null,["# Get a Firestore document using custom types.\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(\"BJ\");\n DocumentSnapshot snapshot = await docRef.GetSnapshotAsync();\n if (snapshot.Exists)\n {\n Console.WriteLine(\"Document data for {0} document:\", snapshot.Id);\n City city = snapshot.ConvertTo\u003cCity\u003e();\n Console.WriteLine(\"Name: {0}\", city.Name);\n Console.WriteLine(\"State: {0}\", city.State);\n Console.WriteLine(\"Country: {0}\", city.Country);\n Console.WriteLine(\"Capital: {0}\", city.Capital);\n Console.WriteLine(\"Population: {0}\", city.Population);\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 docAsEntity(ctx context.Context, client *firestore.Client) (*City, error) {\n \tdsnap, err := client.Collection(\"cities\").Doc(\"BJ\").Get(ctx)\n \tif err != nil {\n \t\treturn nil, err\n \t}\n \tvar c City\n \tdsnap.https://cloud.google.com/go/docs/reference/cloud.google.com/go/firestore/latest/index.html#cloud_google_com_go_firestore_DocumentSnapshot_DataTo(&c)\n \tfmt.Printf(\"Document data: %#v\\n\", c)\n \treturn &c, 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(\"BJ\");\n // asynchronously retrieve the document\n ApiFuture\u003cDocumentSnapshot\u003e future = docRef.get();\n // block on response\n DocumentSnapshot document = future.get();\n City city = null;\n if (document.exists()) {\n // convert document to POJO\n city = document.toObject(City.class);\n System.out.println(city);\n } else {\n System.out.println(\"No such document!\");\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 $city = City::fromArray($snapshot-\u003edata());\n\n if ($snapshot-\u003eexists()) {\n printf('Document data:' . PHP_EOL);\n print((string) $city);\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(\"BJ\")\n\n doc = doc_ref.get()\n city = City.from_dict(doc.to_dict())\n print(city)\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)."]]