Skip to content

Order diff between backend and client with order_by_key() #677

Description

@tperraut

Describe your environment

  • Operating System version: macOS 12.4
  • Firebase SDK version: 6.1.0
  • Firebase Product: database
  • Python version: 3.7.9
  • Pip version: 21.3.1

Describe the problem

When using order_by_key(), the order of the returned OrderedDict is not the same as the database making pagination very difficult

Steps to reproduce:

  • Create a collection with this json in "/test" for example :
{
  "123": {"myValue": true},
  "100001": {"myValue": true},
  "100002": {"myValue": true},
  "100003": {"myValue": true},
  "100004": {"myValue": true},
  "100005": {"myValue": true}
}
  • Query it with :
db.reference("/test").order_by_key().limit_to_first(4).get()
  • Comparison :

Current :

OrderedDict([('100001', {'myValue': True}), ('100002', {'myValue': True}), ('100003', {'myValue': True}), ('123', {'myValue': True})])

Expected :

OrderedDict([('123', {'myValue': True}), ('100001', {'myValue': True}), ('100002', {'myValue': True}), ('100003', {'myValue': True})])

Comments

So according to this link, order should be the same as the collection but in the corresponding OrderedDict the order is not the same so if you want make a pagination strategy, it just loops as getting the last key of the OrderedDict returned and using it in start_at will return the same result over and over again.

Activity

  1. tperraut commented on Mar 2, 2023

    @tperraut
    Author

    We fixed this issue on a fork replacing this line :

    if isinstance(result, (dict, list)) and self._order_by != '$priority':

    by

    if isinstance(result, (dict, list)) and self._order_by != '$priority' and self._order_by != '$key':

    So the client sorter is not triggered when ordering by key and we will have the same order as the database given here instead of a pure lexicographic one

  2. tperraut commented on Sep 2, 2026

    @tperraut
    Author

    up

  3. added 2 commits that reference this issue on Sep 10, 2026
    94368ed
    d1f212a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions