Add a Firestore document using an autogenerated id

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

Add a Firestore document using an autogenerated id

Explore further

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

Code sample


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);


_, _, 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)


// 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());


// 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: ',;


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


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


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.