Add a Firestore document using a custom type

Stay organized with collections Save and categorize content based on your preferences.

Add a Firestore document using a custom type

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample


DocumentReference docRef = db.Collection("cities").Document("LA");
City city = new City
    Name = "Los Angeles",
    State = "CA",
    Country = "USA",
    Capital = false,
    Population = 3900000L
await docRef.SetAsync(city);


city := City{
	Name:    "Los Angeles",
	Country: "USA",
_, err := client.Collection("cities").Doc("LA").Set(ctx, city)
if err != nil {
	// Handle any errors in an appropriate way, such as returning them.
	log.Printf("An error has occurred: %s", err)


City city =
    new City("Los Angeles", "CA", "USA", false, 3900000L, Arrays.asList("west_coast", "socal"));
ApiFuture<WriteResult> future = db.collection("cities").document("LA").set(city);
// block on response if required
System.out.println("Update time : " + future.get().getUpdateTime());


city = City(name=u'Los Angeles', state=u'CA', country=u'USA')

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.