0

First of all, I already saw : this answer, and other answers.They don't responde to my question.

I haee a pretty long list of events:

{
  "event1": {
  "date" : 1420753443,
  "name" : "name1"
  },
  "event2": {
  "date" : 1429053443,
  "name" : "name2"
  }
}

I need (if possible) to get the events between two indexes (not dates), but ordered by date.

For example, I tried to intersect two lists in order to accomplish my goal:

 let query = this.databaseRef
  .orderByChild('date')
  .limitToFirst(100)
  .limitToLast(100);

but an error is thrown

Also, some events can be at the same time. Because of that, I can't use the last date on the previous page to query the events for the next page

 let query = this.databaseRef
  .orderByChild('date')
  .startAt(oldDate)
  .limitToFirst(10);

My purpose is to get from firebase as much items as needed to be sown on user's screen: first the items in range 0-9, second 10-19 etc..

Alex Huiculescu
  • 129
  • 2
  • 11
  • What do you mean by _two indexes (not dates)_? So you want for example first 100 and last 100 records? – bennygenel Sep 27 '17 at 13:35
  • I want the items between two positions. Since the query is ordered, I would like to get the items from the sixth item to the tenth. – Alex Huiculescu Sep 27 '17 at 13:39
  • Something like: startAtIndex() and endAtIndex() – Alex Huiculescu Sep 27 '17 at 13:40
  • Firebase doesn't have something like a `skip()` method for queries. So you might need to execute more than one queries. For example, you might need to get the sixth record first and then tenth record and use `startAt` and `endAt` together to get the records between them. But this would be a really bad thing for performance. For a different idea maybe you can store the record order and query with that value – bennygenel Sep 27 '17 at 13:56
  • @bennygenel skip() is what i looked for. I finished using an EventManager. It keeps the list of events inernally. First, it queries first 50 iterms, that's about 10 pages for me. Then, in background, it queries the other items. I have no performance issues doing so – Alex Huiculescu Sep 27 '17 at 14:39

1 Answers1

0

You can use startAt and endAt methods with the interval of indexes you want to query

Simple usage:

// Find all dinosaurs that are at least three meters tall.
var ref = firebase.database().ref("dinosaurs");
ref.orderByChild("height").startAt(3).on("child_added", function(snapshot) {
  console.log(snapshot.key)
});

If you need further information, you can check this doc: https://firebase.google.com/docs/reference/js/firebase.database.Query#startAt

Hope it helps

soutot
  • 3,531
  • 1
  • 18
  • 22
  • startAt(3) with orderByChild("height") will ignore the events with height < 3, and I want to operate on indexes, not on child values. Sorry, it does not – Alex Huiculescu Sep 27 '17 at 14:29