[Solved] auto generate id for document and not collection in firestore

I have gone through the firestore docs and I’m yet to find an example where we have something like this.

collection
       |--document
                |--{auto-generated-id}
                                  |--property1:value1
                                  |--property2:value2
                                  |--peoperty3:value3

Rather what I often see is:

collection     
         |--{auto-generated-id}
                            |--property1:value1
                            |--property2:value2
                            |--peoperty3:value3

In the former, I cannot call add()-(which generates unique id) on a document. However this can be done in a collection as shown in the latter sketch above.

My question is thus:
Is there a way firestore can help autogenerate an id after creating a document
i.e
How can I achieve something like this:

db.collection("collection_name").document("document_name").add(object)
Enquirer: Urchboy

||

Solution #1:

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);
Respondent: Alex Mamo

Solution #2:

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(function(docRef) {
    console.log("Document written with ID: ", docRef.id);
})
.catch(function(error) {
    console.error("Error adding document: ", error);
});

it’s more convenient to let Cloud Firestore auto-generate an ID for you. You can do this by calling add()

Read more about it on Add data to Cloud Firestore

Respondent: Balalen

Solution #3:

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.

Respondent: Doug Stevenson

Solution #4:

To achieve this in Flutter/dart you can make a Future to 1). Create a document in firestore (firestore will auto generate an ID for the document), 2). Then call the created document, 3). And finally display the id OR Update the ID field

Future createPost(PostModel postModel) async {
  _firestore.collection("posts").add({

  "pid": postModel.pid, // Creating a field for post ID

  }).then((querySnapshot) { // Here whe call the document just after creation

  String generatedID = querySnapshot.id.toString(); // Here we call the ID of the document

  print("ID OF CREATE POST: " + generatedID); // Here we display the generated document ID

  querySnapshot.update({ // Here we update the pid(Post ID) field in the new generated document
    "pid": generatedID
  });
});

}

Respondent: Morne

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.