【问题标题】:Get path from firestore.DocumentRefence从 firestore.DocumentReference 获取路径
【发布时间】:2018-07-22 08:47:41
【问题描述】:

有没有办法从文档引用和集合引用中获取它指向的路径?

当您创建文档参考时,您可以执行以下操作: 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

【问题讨论】:

    标签: javascript python google-cloud-firestore


    【解决方案1】:

    所以我搜索了一段时间,因为我在我的程序中使用了很多参考资料......而且我什至在 firebase 网站上也找不到任何文档

    假设你有一个文档引用对象

    <google.cloud.firestore_v1.document.DocumentReference object at 0x000002702347EC5610>
    
    if you know what collection you can put
    getOriUser = db.collection(u'Users').document(DocumentReference.id)
    OriUser = getOriUser.get()
    Ori= OriUser.to_dict()
    

    【讨论】:

      【解决方案2】:

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

      来自DocumentReference: .路径

      例如var path = ref.path

      来自DocumentSnapshot: .ref.path

      例如var path = doc.ref.path

      【讨论】:

        【解决方案3】:

        我想通了, 因此,如果其他人发现自己遇到了这个问题:

        让obj 为 DocumentReference 类型,db 为 Client 类型

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

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

        path = obj._document_path.replace(db._database_string, '')
        

        【讨论】:

        • _document_path 在 DocumentReference 上未定义。自 2018 年 2 月以来,这种情况发生了变化吗?我们现在如何获取 doc ref 路径?
        • 这些是我目前正在使用的版本,我可以找到路径。 firebase-admin==2.7.0 google-cloud-firestore==0.29.0 你应该得到一个文档参考并从那里拉出来。我强烈建议使用 iPython 并尝试查看您的流程中缺少什么,如果您愿意,请在此处发布您的代码,以便我能够看到。
        • 感谢您的回复 :-) 我能够在 docRef.ref.segments[1] 中获得 DocRef 路径。 0 是集合名称,1 是 Id。
        猜你喜欢
        • 2014-10-16
        • 1970-01-01
        • 2018-07-05
        • 2011-12-11
        • 2011-11-16
        • 2018-08-13
        • 1970-01-01
        • 2014-09-15
        • 2011-12-31
        相关资源
        最近更新 更多