Add a Firestore document using an autogenerated id

Add a Firestore document using an autogenerated id

Explore further

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

Code sample

C#

To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

Dictionary<string, object> city = new Dictionary<string, object>
{
    { "Name", "Tokyo" },
    { "Country", "Japan" }
};
DocumentReference addedDocRef = await db.Collection("cities").AddAsync(city);
Console.WriteLine("Added document with ID: {0}.", addedDocRef.Id);

Go

To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.


import (
	"context"
	"log"

	"cloud.google.com/go/firestore"
)

func addDocWithoutID(ctx context.Context, client *firestore.Client) error {
	_, _, err := client.Collection("cities").Add(ctx, map[string]interface{}{
		"name":    "Tokyo",
		"country": "Japan",
	})
	if err != nil {
		// Handle any errors in an appropriate way, such as returning them.
		log.Printf("An error has occurred: %s", err)
	}

	return err
}

Java

To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

// Add document data with auto-generated id.
Map<String, Object> data = new HashMap<>();
data.put("name", "Tokyo");
data.put("country", "Japan");
ApiFuture<DocumentReference> addedDocRef = db.collection("cities").add(data);
System.out.println("Added document with ID: " + addedDocRef.get().getId());

Node.js

To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

// Add a new document with a generated id.
const res = await db.collection('cities').add({
  name: 'Tokyo',
  country: 'Japan'
});

console.log('Added document with ID: ', res.id);

PHP

To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

$data = [
    'name' => 'Tokyo',
    'country' => 'Japan'
];
$addedDocRef = $db->collection('samples/php/cities')->add($data);
printf('Added document with ID: %s' . PHP_EOL, $addedDocRef->id());

Python

To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

city = {"name": "Tokyo", "country": "Japan"}
update_time, city_ref = db.collection("cities").add(city)
print(f"Added document with id {city_ref.id}")

Ruby

To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

data = {
  name:    "Tokyo",
  country: "Japan"
}

cities_ref = firestore.col collection_path

added_doc_ref = cities_ref.add data
puts "Added document with ID: #{added_doc_ref.document_id}."

What's next

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