Firebase Database Querying 101 - The Firebase Database For SQL Developers #3

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
DAVID EAST: This is a delicate sentence right here. Querying function-- eh. Oh, my god. (LAUGHING) Hey, everyone. Welcome to the third lesson in the Firebase Database for SQL Developers series. And in this lesson, I'm going to teach you all about the basics of querying in the real-time database. But before I begin, I'm going to be upfront with you. If you're coming from a SQL database, you're going to notice a drop off in querying functionality in NoSQL databases, like the Firebase database. So when you're building an application that's backed by a SQL database, to get data from the database to the view, you're going to write a query that joins data from all these different tables, puts it into a subset of data, and then puts that right back to the view. Now, when you're building an application that's backed by a NoSQL database, you tend to structure your data after your views. So all you have to do is simply go read the data, and it has all of the needed parts already available, organized in the database. You take that, and you put it right back into the view. So even if there isn't as much powerful as querying in NoSQL databases, you tend to not need it because you can just structure your data properly. But still, it's really important to know how to query. So let's dive into the laptop and learn the basics of querying in the real-time database. So in SQL, the basics of querying involves two steps. First you select columns from your table, and then second, you restrict using the WHERE clause. In the Firebase database, the basics of querying also follows two steps. First you create a reference to the parent key, and then second, you use an ordering function. And then optionally, you can also use a query function for more advanced restricting. This example query retrieves the first 10 events. The ordering function is order by key. And the querying function is when limitToFirst. And there are actually four different ordering functions that you can use. The first is orderByKey. Using orderByKey allows you to query based upon the child keys which are always string based. And you've seen in the previous videos that these child keys are similar to primary keys in SQL databases. And you would use orderByKey if you're doing any limiting or if you're doing some basic paging. The second is orderByChild. And this is usually what you want. This is very similar to using a WHERE clause. By saying orderByChild, you can specify a child property, like name or age, and then you can query based upon those values. So you can actually say something like, orderByChild name and then dot equal to whatever name you're looking for. Then there's orderByValue, where all the children are ordered by their values. And this works really well if you're dealing with numeric values and you need them to be ordered by their numeric value. And lastly, there is orderByPriority. And I'm not going to get into this because this is sort of the old way of doing things. So anything that you can achieve with orderByPriority, you should really be doing with orderByChild. Now, once you've picked your ordering function, you're able to use one of the more advanced querying functions to further restrict your data. And the querying functions you can use are startAt, endAt, equalTo, limitToFirst, and limitToLast. StartAt and endAt is how you create a range query. You can start at a value or a key and then move until you hit this ending value. EqualTo is like your WHERE clause. You can order by a child of name then say dot equal to, and it's the name you're looking for. LimitToFirst and limitToLast is how you limiting. So limitToFirst would be like saying select these top 10 rows. And limitToLast would be like saying select these bottom 10 or whatever number you need rows. And the great thing about these querying functions is that you can use them all in combination with one another. This SQL query right here will return the first event whose name is Firebase Meetup. So let's take a look at how you would do this with the Firebase database. First we get the database object. And then we're going to create a reference to the parent key which is events. And it's at this point that we can begin our query. So to begin our query, we're going to orderByChild and specify the name property, then make sure that we're using the equalTo querying function, and say the name is equal to Firebase Meetup. And then lastly, we will say limitToFirst and specify one so we get the first event that comes back. So basically, getting the events reference is like your select statement. And then ordering by child and saying equal to is like your WHERE statement. And then the limit is like your limitToFirst method. So that's the basics of querying in the Firebase database. If you like this video, make sure to like and subscribe so you can stay up to date with all of our content and make sure you stay tuned to the next lesson, where we're going to take a look at eight different SQL queries and then convert them one by one to Firebase queries. So that's all. And I will see you in the next lesson. Oh, hey there. Just fixing up new content over on the Firebase channel. Thanks for watching our video. You also might like this one or even this one. And it's always good to subscribe. This thing is awesome.
Info
Channel: Firebase
Views: 184,754
Rating: undefined out of 5
Keywords: google, developer, Firebase, SQL, fullname: David East, GDS: full production, Team: Scalable Advocacy, Type: screencast, Location: MTV, database query, tips, tutorial, lesson, product: Firebase
Id: 3WTQZV5-roY
Channel Id: undefined
Length: 5min 27sec (327 seconds)
Published: Fri Dec 02 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.