从firestore.DocumentRefence获取路径

Elo*_*ati 0 javascript python google-cloud-firestore

有没有一种方法可以从文档引用和集合引用中获得指向它的路径?

当您创建文档引用时,您可以执行以下操作: db.document('your/document/reference/path') 并返回DocumentReference实例<class 'google.cloud.firestore_v1beta1.document.DocumentReference'>

我有相反的需要<class 'google.cloud.firestore_v1beta1.document.DocumentReference'>,我想得到路径your/document/refence/path

gbh*_*all 15

获取文档路径的正确方法:

来自DocumentReference : .path

例如 var path = ref.path

来自DocumentSnapshot .ref.path

例如 var path = doc.ref.path


Elo*_*ati 5

我想通了,所以如果有人发现这个问题:

obj是类型DocumentReference的,而db客户端的类型

然后obj._document_path返回文档的完整路径

对我来说,我只需要路径的最后一部分,就可以完成工作:

path = obj._document_path.replace(db._database_string, '')
Run Code Online (Sandbox Code Playgroud)