Over a million developers have joined DZone.

Firebase Querying Using AngularJS

DZone's Guide to

Firebase Querying Using AngularJS

Applying certain functions in AngularJS makes it really easy to query your database in order to get the specific results that you need.

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

I've already done a couple of posts about Firebase and how to use the real-time database and storage with AngularJS. If you missed it, you can check it out here. Today, I want to go further and give you some details on how to querying.

Understanding the Reference Object

As you already know, we can define a reference object just by calling the following functions in the database object: firebase.database().ref(). This reference object offers you a bunch of functions to query our database paths in order to return just a subset of objects.

Using Orderbychild(), Orderbykey(), Orderbypriority(), and Orderbyvalue()

Using this set of functions allows us to create a new query reference that will be sorted by different properties. For example, child will sort the subset by a specific property within the returned data and key is going to sort the subset by the object key.

Using Startat(), Endat(), and Equalto()

Using this set of functions allow us to generate a query with a subset of an object that matches with the specified conditions. For example, we can use equalTo chained with orderByChild in order to sort the subset by a specific property and then filter this subset to the results that match with an specific condition, as I did in the example below.

function getPlacesByUser(uid) {
    if (!places) {
        places = $firebaseArray(firebaseDataService.places.orderByChild('userId').equalTo(uid));
    return places;

Applying those functions, as you can see, makes it really easy to query our database in order to get the specific results we need. I have a working example in this link if you want to see the complete implementation. In this example, I filter the result by user ID.

It's important to highlight the reference object only accepts one orberByChild function in the chain. That means that you can't use it twice to filter the subset by two different properties. If you want to do that, you need to add a property to the object which is a combination of the properties you want to filter.

For a complete reference of Angular Fire, use this link. For a complete reference of Firebase, use this link. Finally, for a complete list of querying functions, you can use this link.

If you found this post useful, please don't forget to press the like button and share it. If you are in doubt, don't hesitate to ask a question. As always, thank you for reading.

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

firebase ,querying ,angularjs ,mobile

Published at DZone with permission of Ezequiel Reyno. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}