Firebase Querying

Recently I was tasked with creating a simple in-house wiki for my company and I thought this would be a good time to use Google’s Firebase for my back-end. I previously used Firebase to host a few web apps and was happy with the results.
If you’re not familiar with firebase, it’s a mobile and web application development platform from google. It offers a host of features and — best of all- it’s free (although there are fees for advanced features). The database portion of the platform is a NoSql database where data is stored in JSON format.

Querying Data
After the initial setup and once you’ve structured your data, we can begin querying for data. The first thing we’ll do is create a reference to the database
var db = firebase.database();Next, we will reference our parent key by entering the path
var ref = db.ref('playerData/playerName/lebron');Next we will attach an ordering function. Firebase currently has four ordering functions that we can use:
- orderByKey()
- orderByChild()
- orderByValue()
- orderByPriority()
In this example we will be using orderByKey, but the other ordering functions work similarly. Now we will attach the ordering function
ref.orderByKey().on('value',function(snapshot){
var data = snapshot.val();for(key in data){
console.log(data[key));}});
The code above will cycle through all keys on the lebron object and print them out to the console.

by adding a query function, we can be even more specific. Firebase has four query functions that we can use:
- equalTo
- limitToFirst
- limitToLast
- isEqual
- endAt
we will be using the equalTo query function to target apg (assist per game).
ref.orderByKey().equalTo('apg').on('value',function(snapshot){
var data = snapshot.val();for(key in data){
console.log(data[key));}});
that will print out 8.8 to the console.
conclusion
As you can see, querying in Firebase can be extremely easy. Just make sure that your data is structured properly. If not, you can run into time consuming problems.