Lem Zhi San
Lem Zhi San

Reputation: 5

Firestore get the latest document

I wanna compare and find the latest date among all the chat documents. Then get the message and date field from the latest one and display it. May I know is there a way to do this?

firestore chat

Upvotes: 0

Views: 65

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598765

You're looking for a query that is ordered by descending date and then limiting it to one result.

So something like:

Query query = collectionRef.orderBy("date", Direction.DESCENDING).limit(1);

Then you can just read the data once or attach a listener to also get updates.

Upvotes: 4

Related Questions