Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore how to combine orderBy desc with startAfter(null)

I've been searching for this one for a while but have not found a reason why this is the way it is.

Basically this returns an array of documents in the collection.

this.db.collection('people', ref => {
    return ref.orderBy(name, 'asc')
              .startAfter(null)
              .limit(10)
})...

This returns an empty array.

this.db.collection('people', ref => {
    return ref.orderBy(name, 'desc')
              .startAfter(null)
              .limit(10)
})...

The only difference is orderBy is set to 'desc'. Can someone please explain why this happens and a way around it? Thanks in advance!

like image 706
Kyle Abens Avatar asked Oct 23 '25 12:10

Kyle Abens


1 Answers

I believe the issue is startAfter(null). When you sort in ascending order, the null docs come first and then the non-null docs. For the exact order, see the Firebase documentation on the ordering of value types.

When you sort in descending order, all the null docs come last so there is nothing after the null docs for the query to return.

I suggest removing startAfter(null) until you have a value for it.

like image 149
Juan Lara Avatar answered Oct 25 '25 03:10

Juan Lara



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!