4

I'm new to python. I'm using a firebase firestore database for this project. After entering the Admission_No, I want to retrieve all the data in the relevant document such as name, grade, phone. I tried to write a program for it. But I failed. Please help me to complete my project. Thank you all.

The Code might be full of mistakes. Please don't mind it and fix them for me.

Sample Data Sample Data Structure

Here is my Code

.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")
I'm a Studnet
  • 130
  • 1
  • 8

2 Answers2

3

Upon checking your code, you declared another query Name, but you didn't include it in your data dictionary (data). You can remove the query get_data and just use your query_ad to check/get the value of your admission_no:

Untested Code:

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()}')

Since you used the where() to query your documents, you can follow this guide to get multiple documents from a collection. Additionaly, by default, Cloud Firestore retrieves all documents that satisfy the query in ascending order by document ID, but you can order and limit the data returned.

For more references, you can check this guide to perform simple and compound queries in Cloud Firestore

RJC
  • 1,224
  • 2
  • 12
  • It retrieves nothing. The output comes as `enter ad_no : r enter name : r Process finished with exit code 0` It Doesn't work for me. How to resolve it? Is there any other way? or should I change the database. (The document id of my database if custom [like ad_no + name]). Please... – I'm a Studnet Jun 06 '22 at 11:03
  • I edited the code because of a mistake of it. Please check it. – I'm a Studnet Jun 06 '22 at 11:07
  • Can you also share your sample data structure? – RJC Jun 07 '22 at 01:08
  • Okay. I added a picture. Please check the post, Sir. Thank you – I'm a Studnet Jun 08 '22 at 11:38
  • I've updated my posted answer, you can try it out and test it on your end. – RJC Jun 09 '22 at 06:14
  • It still does not work. No any other way? or Should I change my program or add more details. Do you have any suggestions, Sir? – I'm a Studnet Jun 09 '22 at 17:56
  • I suggest that you check the documentation in my answer for further information on how you can query or get data from Firestore. – RJC Jun 10 '22 at 07:45
2

In this case, I tried many ways. But finally, I realized we should provide the correct Document ID for retrieve all fields of data from 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!')

In my case I tried with this Code and it was succeed.

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!')

Thank you everyone who helped me with this issue. (@RJC)

I'm a Studnet
  • 130
  • 1
  • 8