【问题标题】:Get data of a document in firestore by giving a field value belonged to that document using Python?通过使用Python提供属于该文档的字段值来获取firestore中文档的数据?
【发布时间】:2022-06-16 19:21:37
【问题描述】:

我是python 的新手。我正在为这个项目使用firebase firestore 数据库。输入Admission_No后,我想检索namegradephone等相关文档中的所有数据。我试图为它编写一个程序。但我失败了。请帮助我完成我的项目。谢谢大家。

本准则可能充满错误。请不要介意并为我修复它们。

样本数据

这是我的代码

.py

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore

cred = credentials.Certificate("cred.json")
firebase_admin.initialize_app(cred)

db = firestore.client()

data = {
    'Admission_No': input('enter ad_no : ')
}

query_ad = db.collection(u'Users').where(u"Admission_No", u"==", data["Admission_No"]).get()

get_data = db.collection(u'Users').where(u"Name", u"==", data["Name"]).get()

if query_ad:
    print('Exist')
    print(get_data)

else:
    print("Doesn't Exist")

【问题讨论】:

    标签: python database firebase google-cloud-firestore data-retrieval


    【解决方案1】:

    在检查您的代码后,您声明了另一个查询 Name,但您没有将它包含在您的数据字典 (data) 中。您可以删除查询 get_data 并使用您的 query_ad 检查/获取您的 admission_no 的值:

    未经测试的代码:

    import firebase_admin
    from firebase_admin import credentials
    from firebase_admin import firestore
    
    cred = credentials.Certificate("cred.json")
    firebase_admin.initialize_app(cred)
    
    db = firestore.client()
    
    data = {
        'Admission_No': input('enter ad_no : ')
    }
    
    query_ad = db.collection(u'users').where(u"Admission_No", u"==", data["Admission_No"]).stream()
    
    for doc in query_ad:
        print(f'{doc.id} => {doc.to_dict()}')
    

    由于您使用where() 来查询您的文档,您可以按照此指南到get multiple documents from a collection。另外,默认情况下,Cloud Firestore 按文档 ID 升序检索满足查询的所有文档,但您可以order and limit the data returned

    如需更多参考,您可以查看此guide 以在 Cloud Firestore 中执行简单和复合查询

    【讨论】:

    • 它什么也没得到。输出为enter ad_no : r enter name : r Process finished with exit code 0 它对我不起作用。如何解决?还有其他方法吗?或者我应该更改数据库。 (如果自定义 [like ad_no + name],则为我的数据库的文档 ID)。请...
    • 我编辑了代码,因为它有一个错误。请检查一下。
    • 能否分享一下你的示例数据结构?
    • 好的。我添加了一张图片。请检查帖子,先生。谢谢
    • 我已经更新了我发布的答案,你可以试试看。
    【解决方案2】:

    在这种情况下,我尝试了很多方法。但最后,我意识到我们应该提供正确的Document ID 来检索来自firestore 的所有数据字段。

    Getting Data from Firestore - Method given in firestore documentation

    doc_ref = db.collection(u'cities').document(u'SF')
    
    doc = doc_ref.get()
    if doc.exists:
        print(f'Document data: {doc.to_dict()}')
    else:
        print(u'No such document!')
    

    就我而言,我尝试使用此代码并成功。

    u_name = input('Enter User Name : ')
    ad_no = input('Enter Admission Number : ')
    
    doc_name = ad_no + " - " + u_name
    
    doc_ref = self.db.collection(u'Users').document(doc_name)
    
    doc = doc_ref.get()
    
    if doc.exists:
        Full_Name = str(doc.to_dict()['Full_Name'])
        Admission_No = str(doc.to_dict()['Admission_No'])
        Grade = str(doc.to_dict()['Grade'])
        Phone_Number = str(doc.to_dict()['Phone_Number'])
    
        print(Full_Name, Admission_No, Grade, Phone_Number)
    
    else:
        print('No such document!')
    

    感谢所有帮助我解决此问题的人。 (@RJC)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-11
      • 2018-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多