Home > Back-end >  Firestore How to retrieve document's autogenerated id in the past?
Firestore How to retrieve document's autogenerated id in the past?

Time:01-06

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

CodePudding user response:

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;
  • Related