问题
I have a list of chat messages that are ordered by timestamp and have a unique firebase key (made by the push operator)
In this list I want to get the message at position 100 from the end without knowing its key. Is there a way to do this?
I tried using the filtering methods limitToLast() and limitToFirst together which created an error, saying a filter was already set.
The current way I do it is by downloading all 100 messages with:
admin.database().ref("path/to/messages").limitToLast(100)
And then I look into that list for the first key which is the one I want.
Is ther a way that I can get the 100th message without downloading all the other messages as well?
回答1:
Firebase Realtime Database queries don't support specifying an offset, but instead require that you know the key values of the anchor document. If you don't know those, you will have to read all documents before/after the one you search for.
Also see:
- How to change limitToFirst() dynamically in recyclerview firebase?
- AngularJS : Fetch records for pagination from Firebase
- how to read firebase database
- Firebase paging data. Is it possible?
回答2:
citiesRef.orderBy("population").startAt(1000000)
Source: Paginate data with query cursors
来源:https://stackoverflow.com/questions/57570929/is-there-a-way-to-get-a-firebase-element-from-a-certain-index