Home > Net >  How to get the document id which consists that collection while making collection group query?
How to get the document id which consists that collection while making collection group query?

Time:08-18

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 the getParent() method of the DocumentReference
  • Secondly, on this parent CollectionReference, call the getParent() method which returns the DocumentReference of the Collection's parent Document.
  • Finally, use the getId() method on this DocumentReference. You're done.
  • Related