Increment a Firestore document field

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

Update a Firestore document field using Increment

Explore further

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

Code sample


import (


// updateDocumentIncrement increments the population of the city document in the
// cities collection by 50.
func updateDocumentIncrement(projectID, city string) error {
	// projectID := "my-project"

	ctx := context.Background()

	client, err := firestore.NewClient(ctx, projectID)
	if err != nil {
		return fmt.Errorf("firestore.NewClient: %v", err)
	defer client.Close()

	dc := client.Collection("cities").Doc(city)
	_, err = dc.Update(ctx, []firestore.Update{
		{Path: "population", Value: firestore.Increment(50)},
	if err != nil {
		return fmt.Errorf("Update: %v", err)

	return nil


DocumentReference washingtonRef = db.collection("cities").document("DC");

// Atomically increment the population of the city by 50.
final ApiFuture<WriteResult> updateFuture =
    washingtonRef.update("population", FieldValue.increment(50));


// ...
const washingtonRef = db.collection('cities').doc('DC');

// Atomically increment the population of the city by 50.
const res = await washingtonRef.update({
  population: FieldValue.increment(50)


$cityRef = $db->collection('samples/php/cities')->document('DC');

// Atomically increment the population of the city by 50.
    ['path' => 'regions', 'value' => FieldValue::increment(50)]


washington_ref = db.collection(u'cities').document(u'DC')

washington_ref.update({"population": firestore.Increment(50)})


city_ref = firestore.doc "#{collection_path}/DC"
city_ref.update({ population: firestore.field_increment(50) })

What's next

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