Retrieving Data
This document covers the basics of retrieving database data, how data is ordered, and how to perform simple queries on data. Data retrieval in the Admin SDK is implemented slightly differently across different programming languages.
- Asynchronous listeners: Data stored in a Firebase Realtime Database is retrieved by attaching an asynchronous listener to a database reference. The listener is triggered once for the initial state of the data and again anytime the data changes. An event listener may receive several different types of events. This mode of data retrieval is supported in Java, Node.js and Python Admin SDKs.
- Blocking reads: Data stored in a Firebase Realtime Database is retrieved by invoking a blocking method on a database reference, which returns the data stored at the reference. Each method call is a onetime operation. That means the SDK does not register any callbacks that listen to subsequent data updates. This model of data retrieval is supported in Python and Go Admin SDKs.
Getting Started
Let's revisit the blogging example from the previous article to understand how to read data from a Firebase database. Recall that the blog posts in the example app are stored at the database URL https://docs-examples.firebaseio.com/server/saving-data/fireblog/posts.json.
If you run the above code, you'll see an object containing all your posts logged to the console. In case of Node.js and Java, the listener function is called anytime new data is added to your database reference, and you don't need to write any extra code to make this happen.
In Java and Node.js, the callback function receives a DataSnapshot
, which is a snapshot of the data. A snapshot is a picture of the data at a particular database reference at a single point in time. Calling val()
/ getValue()
on a snapshot returns the a language-specific object representation of the data. If no data exists at the reference's location, the snapshot's value is null
. The get()
method in Python returns a Python representation of the data directly. The Get()
function in Go unmarshals the data into a given data structure.
Notice that we used the value
event type in the example above, which reads the entire contents of a Firebase database reference, even if only one piece of data changed. value
is one of the five different event types listed below that you can use to read data from the database.
Read Event Types in Java
Value
The value
event is used to read a static snapshot of the contents at a given database path, as they existed at the time of the read event. It is triggered once with the initial data and again every time the data changes. The event callback is passed a snapshot containing all data at that location, including child data. In the code example above, value
returned all of the blog posts in your app. Everytime a new blog post is added, the callback function will return all of the posts.
Child Added
The child_added
event is typically used when retrieving a list of items from the database. Unlike value
which returns the entire contents of the location, child_added
is triggered once for each existing child and then again every time a new child is added to the specified path. The event callback is passed a snapshot containing the new child's data. For ordering purposes, it is also passed a second argument containing the key of the previous child.
How Data is Ordered
This section explains how your data is ordered when using each of the four ordering functions.
orderByChild
When using orderByChild()
, data that contains the specified child key is ordered as follows:
- Children with a
null
value for the specified child key come first. - Children with a value of
false
for the specified child key come next. If multiple children have a value offalse
, they are sorted lexicographically by key. - Children with a value of
true
for the specified child key come next. If multiple children have a value oftrue
, they are sorted lexicographically by key. - Children with a numeric value come next, sorted in ascending order. If multiple children have the same numerical value for the specified child node, they are sorted by key.
- Strings come after numbers, and are sorted lexicographically in ascending order. If multiple children have the same value for the specified child node, they are ordered lexicographically by key.
- Objects come last, and sorted lexicographically by key in ascending order.
orderByKey
When using orderByKey()
to sort your data, data is returned in ascending order by key as follows. Keep in mind that keys can only be strings.
- Children with a key that can be parsed as a 32-bit integer come first, sorted in ascending order.
- Children with a string value as their key come next, sorted lexicographically in ascending order.
orderByValue
When using orderByValue()
, children are ordered by their value. The ordering criteria is the same as in orderByChild()
, except the value of the node is used instead of the value of a specified child key.
0 Comments