Skip to main content

References and paths

A reference points to a specific location in your database. You use references to read, write, and listen to data at that location.

Creating a reference

Call db.ref(path) with a slash-separated path string:
Call db.ref() with no arguments (or an empty string) to get a reference to the root of your database:
References are lightweight objects. Creating a reference does not fetch any data or open any connections. It simply represents a path. You can create as many as you need without any performance cost.
You can move around the data tree from any reference.

.child(path)

Returns a reference to a child location:
You can also pass deeper paths:

.parent

Returns a reference to the parent location. Returns null for the root reference.

.root

Returns a reference to the root of the database, no matter where you start:

Properties

.key

The last segment of the path. For root references, this is null.

.path

The full path string from the root:

Practical example

References make it easy to work with related data without repeating path strings:
Store references in variables when you use the same path repeatedly. It keeps your code cleaner and avoids typos in path strings.