Firebase Firestore Get Data from Collection

Firebase Firestore get data from collection

The get() operation returns a Task<> which means it is an asynchronous operation. Calling getListItems() only starts the operation, it does not wait for it to complete, that's why you have to add success and failure listeners.

Although there's not much you can do about the async nature of the operation, you can simplify your code as follows:

private void getListItems() {
mFirebaseFirestore.collection("some collection").get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot documentSnapshots) {
if (documentSnapshots.isEmpty()) {
Log.d(TAG, "onSuccess: LIST EMPTY");
return;
} else {
// Convert the whole Query Snapshot to a list
// of objects directly! No need to fetch each
// document.
List<Type> types = documentSnapshots.toObjects(Type.class);

// Add all to your list
mArrayList.addAll(types);
Log.d(TAG, "onSuccess: " + mArrayList);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(), "Error getting data!!!", Toast.LENGTH_LONG).show();
}
});
}

How to retrieve data from firestore and store it to array (Kotlin- Android Studio)

From firestore documentation - https://firebase.google.com/docs/firestore/query-data/get-data#kotlin+ktx_3

You need to create reference of the firestore database. Then get the data from the collection and this will return you all the documents of a collection. In iteration convert the documents to the user class that you need to create which will have email field.

 db.collection("users")
.get()
.addOnSuccessListener { result ->
for (document in result) {
val users = document.toObject(Users::class.java)
Log.d(TAG, "${document.id} => ${document.data}")
}
}
.addOnFailureListener { exception ->
Log.w(TAG, "Error getting documents: ", exception)
}

Get data from sub-collections in Firebase Firestore using nested query

If you want to search all userFlights collections, you can use a collection group query.

You can get all documents from all userFlights collections with:

getDocs(collectionGroup(db, "userFlights"));

Get data of a document in firestore by giving a field value belonged to that document using Python?

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)

How Can I Retrieve these data from Firestore?

That seems to be a single document. You would have to fetch the whole document if you are just going to have a new map for each date. Then sort it yourself using dart.

FirebaseFirestore.instance
.collection('BMI')
.doc('docID')
.get()
.then((DocumentSnapshot documentSnapshot) {
if (documentSnapshot.exists) {
print('Document exists on the database');
// sort here

List myList = documentSnapshot.data!.data().entries.map((entry) => entry).toList();
print(myList);
// sorting the list
print(myList.sort((a, b) => a.date.compareTo(b.date)))
}
});

You can read more about sorting lists here. If you need to limit number of entries to be fetched then you would have create a dedicated documents for each entry.

Retrieve data from a specific document only within a collection - Firestore

If you only want to read a single document:

final CollectionReference categoriesDetails =
FirebaseFirestore.instance.collection("collection-name");
final DocumentReference categoryDetails = categoriesDetails.document("barber");

You can then use the categoryDetails in the StreamBuilder in your UI, and will get a single DocumentSnapshot rather than a QuerySnapshot with multiple documents.



Related Topics



Leave a reply



Submit