Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is It Possible To Get The ID Before It Was Added?

I know that in Realtime Database I could get the push ID before it was added like this:

 DatabaseReference databaseReference= FirebaseDatabase.getInstance().getReference();  String challengeId=databaseReference.push().getKey(); 

and then I could add it using this ID.

Can I get it also in the Cloud Firestore?

like image 338
Tal Barda Avatar asked Oct 20 '17 08:10

Tal Barda


People also ask

How do I find my generated document ID in firestore?

Set the data of a document within a collection, explicitly specifying a document identifier. Add a new document to a collection. In this case, Cloud Firestore automatically generates the document identifier. Create an empty document with an automatically generated identifier, and assign data to it later.


2 Answers

This is covered in the documentation. See the last paragraph of the add a document section.

DocumentReference ref = db.collection("my_collection").doc(); String myId = ref.id; 
like image 95
34m0 Avatar answered Sep 21 '22 03:09

34m0


const db = firebase.firestore(); const ref = db.collection('your_collection_name').doc(); const id = ref.id; 
like image 35
Alberto Di Gioacchino Avatar answered Sep 21 '22 03:09

Alberto Di Gioacchino