Usa un tipo personalizzato sul client per i documenti Firestore
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Usa un tipo personalizzato sul client per i documenti Firestore
Per saperne di più
Per la documentazione dettagliata che include questo esempio di codice, vedi quanto segue:
Esempio di codice
Salvo quando diversamente specificato, i contenuti di questa pagina sono concessi in base alla licenza Creative Commons Attribution 4.0, mentre gli esempi di codice sono concessi in base alla licenza Apache 2.0. Per ulteriori dettagli, consulta le norme del sito di Google Developers. Java è un marchio registrato di Oracle e/o delle sue consociate.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis document provides examples of how to define a custom \u003ccode\u003eCity\u003c/code\u003e type for use with Firestore documents in various languages, including C#, Go, Java, PHP, and Python.\u003c/p\u003e\n"],["\u003cp\u003eEach language example demonstrates how to structure the \u003ccode\u003eCity\u003c/code\u003e class or struct with properties like name, state, country, capital status, population, and in some cases, regions or density.\u003c/p\u003e\n"],["\u003cp\u003eThe document highlights the importance of setting up Application Default Credentials for Firestore authentication, linking to further information on local development environment setup.\u003c/p\u003e\n"],["\u003cp\u003eEach language example, including C#, Go, Java, PHP and Python, has a class for the City object that can then be used for Firestore documents.\u003c/p\u003e\n"],["\u003cp\u003eThe document also directs users to the Google Cloud sample browser for exploring code samples for other Google Cloud products.\u003c/p\u003e\n"]]],[],null,["# Use a custom type on the client for Firestore documents\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- [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 [FirestoreData]\n public class City\n {\n [FirestoreProperty]\n public string Name { get; set; }\n\n [FirestoreProperty]\n public string State { get; set; }\n\n [FirestoreProperty]\n public string Country { get; set; }\n\n [FirestoreProperty]\n public bool Capital { get; set; }\n\n [FirestoreProperty]\n public long Population { get; set; }\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 // City represents a city.\n type City struct {\n \tName string `firestore:\"name,omitempty\"`\n \tState string `firestore:\"state,omitempty\"`\n \tCountry string `firestore:\"country,omitempty\"`\n \tCapital bool `firestore:\"capital,omitempty\"`\n \tPopulation int64 `firestore:\"population,omitempty\"`\n \tDensity int64 `firestore:\"density,omitempty\"`\n \tRegions []string `firestore:\"regions,omitempty\"`\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 public City() {\n // Must have a public no-argument constructor\n }\n\n // Initialize all fields of a city\n public City(\n String name,\n String state,\n String country,\n Boolean capital,\n Long population,\n List\u003cString\u003e regions) {\n this.name = name;\n this.state = state;\n this.country = country;\n this.capital = capital;\n this.population = population;\n this.regions = regions;\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 class City\n {\n /** @var string */\n public $name;\n /** @var string */\n public $state;\n /** @var string */\n public $country;\n /** @var bool */\n public $capital;\n /** @var int */\n public $population;\n /** @var array\u003cstring\u003e */\n public $regions;\n\n /**\n * @param array\u003cstring\u003e $regions\n */\n public function __construct(\n string $name,\n string $state,\n string $country,\n bool $capital = false,\n int $population = 0,\n array $regions = []\n ) {\n $this-\u003ename = $name;\n $this-\u003estate = $state;\n $this-\u003ecountry = $country;\n $this-\u003ecapital = $capital;\n $this-\u003epopulation = $population;\n $this-\u003eregions = $regions;\n }\n\n /**\n * @param array\u003cmixed\u003e $source\n */\n public static function fromArray(array $source): City\n {\n // implementation of fromArray is excluded for brevity\n # ...\n }\n\n /**\n * @return array\u003cmixed\u003e\n */\n public function toArray(): array\n {\n // implementation of toArray is excluded for brevity\n # ...\n }\n\n public function __toString()\n {\n // implementation of __toString is excluded for brevity\n # ...\n }\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 class City:\n def __init__(self, name, state, country, capital=False, population=0, regions=[]):\n self.name = name\n self.state = state\n self.country = country\n self.capital = capital\n self.population = population\n self.regions = regions\n\n @staticmethod\n def from_dict(source):\n # ...\n\n def to_dict(self):\n # ...\n\n def __repr__(self):\n return f\"City(\\\n name={self.name}, \\\n country={self.country}, \\\n population={self.population}, \\\n capital={self.capital}, \\\n regions={self.regions}\\\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)."]]