Skip to main content

Queries

Queries let you sort, filter, and limit the data returned from a reference. All query methods are chainable and return a new query. They don’t modify the original reference.

Ordering

Every query starts with an ordering method. You can only use one orderBy per query.

orderByChild(path)

Sort by a child key’s value:
You can also order by nested child paths:

orderByKey()

Sort by each child’s key (alphabetically):

orderByValue()

Sort by each child’s value directly. Useful when children are primitives (numbers, strings) rather than objects:

orderByPriority()

Sort by the priority set on each child node:

Limiting results

limitToFirst(count)

Returns only the first count items in the ordered result:

limitToLast(count)

Returns only the last count items in the ordered result:

Range filters

Range methods narrow results to a specific window. They work with the active ordering.

startAt(value, key?)

Include items with a value greater than or equal to the specified value:

startAfter(value, key?)

Include items with a value strictly greater than the specified value:

endAt(value, key?)

Include items with a value less than or equal to the specified value:

endBefore(value, key?)

Include items with a value strictly less than the specified value:

equalTo(value, key?)

Match items with exactly the specified value:
The optional key parameter in range methods is used to disambiguate when multiple children have the same value. It acts as a secondary sort by key.

Query identifier

Each query has a queryIdentifier property, a string that uniquely identifies the combination of ordering, limits, and ranges. This is useful for deduplication or caching:

Using queries with subscriptions

Queries work with both once() and on(). This is where they really shine: you can subscribe to a filtered, sorted slice of your data in real time.

Examples

Leaderboard

Display a live top-10 leaderboard sorted by score:

Pagination

Load data page by page using startAfter() and limitToFirst():
You can only use one orderBy method per query. Calling a second orderBy will throw an error. If you need to filter on multiple fields, restructure your data so a single ordering covers your use case, or filter client-side after fetching.