[SOLVED] Get collection name from firebase document


Is there a way I can get the name of the collection a Firestore document is stored in based off a variable of type QueryDocumentSnapshot<DocumentData>? I’m using Firebase v9.


You can use .ref property of QueryDocumentSnapshot to get that document’s DocumentReference and get the collection reference by using .parent as shown below:

const snap = "" // The QueryDocumentSnapshot

console.log("Collection name:", snap.ref.parent.id)

If that document is a part of sub-collection, then it’ll log name of that sub-collection. You can chain .ref.parent again to get parent collection of the same.

Answered By – Dharmaraj

Answer Checked By – Jay B. (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *