Create a Firestore document reference with an autogenerated id

Create a Firestore document reference with an autogenerated id

Code sample

Go

ref := client.Collection("cities").NewDoc()

// later...
_, err := ref.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

// Add document data after generating an id.
DocumentReference addedDocRef = db.collection("cities").document();
System.out.println("Added document with ID: " + addedDocRef.getId());

// later...
ApiFuture<WriteResult> writeResult = addedDocRef.set(data);

Node.js

const newCityRef = db.collection('cities').doc();

// Later...
const res = await newCityRef.set({
  // ...
});

Python

new_city_ref = db.collection(u'cities').document()

# later...
new_city_ref.set({
    # ...
})