How to retrieve the last document in a firebase collection. I would also like to get the document fields value

Viewed 9228

How to retrieve the last document from a Cloud Firestore collection? I would also like to get the document fields value.

Here this the image below:

enter image description here

1 Answers

How to retrieve the last document in a firebase collection?

To solve this, you need to use a query that should look like this:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
Query query = rootRef.collection("High Volume")
    .orderBy("Time", Query.Direction.DESCENDING)
    .limit(1);
query.get().addOnCompleteListener(/* ... */);

This query will order the results according to the time property and will limit the results to one.

Related