codelover1
codelover1

Reputation: 171

How to limit query from the end. Firestore

const messagesRef = collection(db, 'messages')
const q = query(messagesRef, orderBy('createdAt', 'asc'), limit(25))

Im querying documents in 'messages' collection by creation time, the problem with this is that when im trying to limit documents, it limits from the start, but i want to limit it from the end. I want that user would see newer messages but not the old ones.

Upvotes: 0

Views: 664

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599716

To get the last 25 items, use limitToLast(25) instead of limit(25). While that function isn't mentioned in the guide on limiting data, you can find it in the reference documentation.

Upvotes: 2

Related Questions