Auto Generate Id For Document And Not Collection In Firestore
Answer : If you are using CollectionReference's add() method, it means that it: Adds a new document to this collection with the specified POJO as contents, assigning it a document ID automatically. If you want to get the document id that is generated and use it in your reference, then use DocumentReference's set() method: Overwrites the document referred to by this DocumentRefere Like in following lines of code: String id = db.collection("collection_name").document().getId(); db.collection("collection_name").document(id).set(object); Since you already know the id of the document, just call set() instead of add() . It will create the document if it doesn't already exist. This answer might be a little late but you can look at this code here which will generate a new document name: // Add a new document with a generated id. db.collection("cities").add({ name: "Tokyo", country: "Japan" }) .then...