Reputation: 273
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!
Upvotes: 4
Views: 2908
Reputation: 1740
if anyone interested with my solution just arrow up:
var lastDocumentSnapshot : DocumentSnapshot? = null;
if (url != null){
val query = this.getLastDocumentSnapshotAsync(url).await()
if (!query.isEmpty){
lastDocumentSnapshot = query.documents.distinct().first()
}
}
val query = firebaseDb.collection("urls")
.orderBy("name", sortDirection)
.limit(limit)
if (sortDirection == Query.Direction.ASCENDING)
query.startAfter(lastDocumentSnapshot)
else
query.endBefore(lastDocumentSnapshot)
return query.get().asDeferredAsync()
Don't mind the other parts of the code. basically the fix is to use endBefore
if the sort is descending.
Upvotes: 1
Reputation: 6854
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.
Upvotes: 3