Add a Firestore document

Add a Firestore document

Documentation pages that include this code sample

To view the code sample used in context, see the following documentation:

Code sample

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.