Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get path from firestore.DocumentRefence

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

like image 664
Elon Salfati Avatar asked Feb 12 '18 08:02

Elon Salfati


People also ask

How do I find my firestore collection 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.

How do I get data from a firestore document?

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.

How do I get node data from firestore?

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.


2 Answers

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

like image 149
gbhall Avatar answered Oct 01 '22 19:10

gbhall


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, '')
like image 20
Elon Salfati Avatar answered Oct 01 '22 20:10

Elon Salfati