Jordaninho
Jordaninho

Reputation: 185

Firestore How to retrieve document's autogenerated id in the past?

In Firestore, I have added a single document in a subcollection, I don't know its id because it was added in the past, but I know its path. How to get its id ? Should I save its id at the creation?

Thank you

Upvotes: 0

Views: 60

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83093

I don't know its id because it was added in the past, but I know its path.

If with "I know its path" you refer to the Document path, then the Document ID is the last element of the path, in which elements are separated by a slash. For example rootCollectionName/parentDocId/parentCollectionName/docId.


If with "I know its path" you refer to the path of the subcollection, then you could query the subcollection and take the first document, since there is a single doc in the collection.

For example with Dart:

QuerySnapshot querySnapshot = await Firestore.instance.collection("rootCollectionName/parentDocId/parentCollectionName").get();
var documentID = querySnapshot.docs[0].id;

Upvotes: 1

Related Questions