I am making a collection group query and getting the data by .get method , is there any way to get the document id which consists that collection ?
Collection:Users ------ Doccument:user1 -> Collection:Posts -> Document:post 1,2,3.. ------- Doccument:user2 -> Collection: Posts -> Document:post a,b,c..
So while making the query i am getting post 1,2,3.. & a,b,c..
But how to get the document document id from where a,b,c or 1,2,3 are comming ?
In this case it i want to get user 1 & user 2 as result !
Just let me know if it is possible first !
- Android : Kotlin
CodePudding user response:
For a document of any of the Posts
subcollections you need to:
- Firstly, get the
CollectionReference
of the Document's parent Collection, with thegetParent()
method of theDocumentReference
- Secondly, on this parent
CollectionReference
, call thegetParent()
method which returns theDocumentReference
of the Collection's parent Document. - Finally, use the
getId()
method on thisDocumentReference
. You're done.