How to use start_at() & end_at() in Firebase query with Python?

Viewed 433

My Firebase realtime database schema:

schema

Let's suppose above Firebase database schema. I want to get data with order_by_key() which after first 5 and before first 10 not more. Range should be 5-10. Like in the image.

My key is always starting with -.

I'm trying this but failed. It returns 0. How can I do this?

snapshot = ref.child('tracks').order_by_key().start_at('-\5').end_at(u'-\10').get()
1 Answers

Firebase queries are based on cursor/anchor values, and not on offsets. This means that the start_at and end_at calls expect values of the thing you order on, so in your keys they expect the keys of those notes.

To get the slice you indicate you'll need:

ref.child('tracks').order_by_key().start_at('-MQJ7P').end_at(u'-MQJ8O').get()

If you don't know either of those values, you can't specify them and can only start from the first item or end on the last item.

The only exception is that you can specify a limit_to_first instead of end_at to get a number of items at the start of the slice:

ref.child('tracks').order_by_key().start_at('-MQJ7P').limit_to_first(5).get()

Alternatively if you know only the key of the last item, you can get the five items before that with:

ref.child('tracks').order_by_key().end_at('-MQJ8O').limit_to_last(5).get()

But you'll need to know at least one of the keys, typically because you've shown it as the last item on the previous page/first item on the next page.

Related