I want to get the pregnancyInfo document ID which is a subcollection from users collection .
why I want to get the document ID? because I want to access the weight subcollection
As you can see from the code below, what am doing is copying and pasting the doc id but each user have a different pregnancy info document ID. So is there a way to retrieve the Document id and store it in a variable?
Future<Widget> getWeight(String weightDate, String weight) async {
String userUid = getUserId();
print('weight date is $weightDate');
QuerySnapshot result = await FirebaseFirestore.instance
.collection('users')
.doc(userUid)
.collection('pregnancyInfo')
.doc("HMEBTKrnOYnmxPmBMHuV")
.collection('weight')
.get();
2
Answers
If I correctly understand your question you need to execute a query on the
pregnancyInfo
sub-collection under theuserUid
document.You will get one or more documents corresponding to one or more "babies".
If you want to query the pregnancy info across all users, you can use a collection group query to read from all
pregnancyInfo
collections at once.Similarly, you can use a collection group query on
weight
to read from all weight collections in one go.