Add a Firestore document

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

Add a Firestore document

Explore further

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

Code sample

C#

await db.Collection("cities").Document("new-city-id").SetAsync(city);

Go

_, err := client.Collection("cities").Doc("new-city-id").Set(ctx, data)
if err != nil {
	// Handle any errors in an appropriate way, such as returning them.
	log.Printf("An error has occurred: %s", err)
}

Java

db.collection("cities").document("new-city-id").set(data);

Node.js

await db.collection('cities').doc('new-city-id').set(data);

PHP

$db->collection('samples/php/cities')->document('new-city-id')->set($data);

Python

db.collection(u'cities').document(u'new-city-id').set(data)

Ruby

city_ref = firestore.doc "#{collection_path}/new-city-id"
city_ref.set data

What's next

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