Is there a way to get from a document reference and a collections reference the path that it points to?
when you create a document reference you do something like:
db.document('your/document/reference/path')
and you get in return a DocumentReference instance <class 'google.cloud.firestore_v1beta1.document.DocumentReference'>
I need it in reverse I have <class 'google.cloud.firestore_v1beta1.document.DocumentReference'>
and I want to get the path your/document/refence/path
Hover your mouse over the path area until you see the pencil. Click the pencil. Copy the entire path (without the first, left-most forward slash) and paste it into the Authorization form in Flatly.
To read a single document, we can use DocumentReference's get() method that returns a Task<DocumentSnapshot>, while reading multiple documents from a collection or Query, we can use Firestore Query's get() method that returns an object of type Task<QuerySnapshot>. Both methods read the data only once.
To read your data from Firestore, use the get() method. To read from a collection, specify a collection() before calling get() . Or, if you need to read from a document, specify a doc() before calling get() . // get collection const users = await db.
The proper way to get a documents' path:
From DocumentReference: .path
e.g.
var path = ref.path
From DocumentSnapshot: .ref.path
e.g.
var path = doc.ref.path
I figured it out,
so if someone else find himself in this problem:
Let obj
be of type DocumentReference, and db
of type Client
then obj._document_path
returns the full path of the document
and for me, I needed only the last part of the path so this did the job:
path = obj._document_path.replace(db._database_string, '')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With