Articles Blog

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

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


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.

49 thoughts on “Firebase Database Querying 101 – The Firebase Database For SQL Developers #3”

  1. All of you who are planning on starting your projects with firebase, should know that you can not use a query like you would in SQL:
    select .. where d=1 AND d=2
    you can only use equal_to once and do the other filtering within the client code.

  2. David, I'm studying Firebase right now, and I think I still miss the big picture.
    It looks like Firebase is very easy to use, flexible, and whatever, but the advantage of NOT having and intermediate server layer makes the whole thing fall short when we want to realize a really secure and protected application.
    For example, what if I would like to prevent users from inserting data on Sundays, or if I have to issue an external web api request to see if the user is allowed to edit data…
    It looks like there is some javascript interpreter, to execute the rules in Firebase, but (please correct me!) I don't think this is done to let developers write really complex scenarios, it would really be overkill.
    Please, can you help me in understanding better?
    Thanks

  3. No.
    These queries are just not responding to my requests! I've tried rectifying every possible error!
    Still i ain't able to get desired result, and retrieve correct data.
    Please help if possible. 🙂

  4. how can I query for a specific node in my realtime database that has been created using push() method using one of the values stored within that node(e.g. city = new york),
    Will the following query work:
    mRef.child("restaurants").orderByChild("city").equalTo("New York");

  5. When I add ".orderByChild('myproperty')" to my query, I get an error" Using an unspecified index. Consider adding ".indexOn": "myproperty"? Can't I use this without messing with rules? Not ready for that just yet…

  6. how to display recently added data to database on top once you call from database? I mean recently added data first in the list view.

  7. Am i the only one to spot the SQL queries use "==" which is incorrect. ?
    SQL syntax is just a single "="

  8. Nicely presented but VERY disappointing. The last query was a joke. Where are the LIKE statements? Firebase team, PLEASE sort this out. What you're offering here just isn't up to the job.

  9. Hey dude . Would you mind to make tutorial about How to Filter Firebase Query RecyclerView with SearchView ?

  10. Firebase looks cool, but am finding a big problem implementing %like% for searching as it is easily done in sql. can some one help me please

  11. Hello sir, thank you for your explanation, really help me as junior programmer. Can you explain me how to convert query like this one:

    SELECT * FROM Event WHERE name="a" AND job="b"

    into query format in firebase?
    Thank you…

  12. So for more advanced filtering we need to send all the objects in the child to the client and have their copy of the code do the work? If that is the case wouldn't there be security issues? Like if I have an array of sensitive documents assigned to different people but also could be assigned to more than one person I would have to send a list of all the documents and then trust that the client code sorts out only the ones they are allowed to view. Would it not be better for the data server to only send what is necessary? I'll keep watching and maybe the answer will show up.

  13. how can i use orderByKey and orderByChild in the same query?
    For example : i need to get next question of specific category (1). but i know current question id value (10).
    “`firebase.database().ref('/questions').orderByKey()
    .startAt("10")
    .orderByChild('category_id')
    .startAt("1")
    .limitToFirst(1).once('value').then(function(snapshot)“`

  14. when the damn hipsters gather in one place they create something like Firebase. I can't even imagine how it got popular. I'm praying each day so there could be someone who can create something like Parse. When comparing Parse vs Backendless vs Firebase it is same if you compare JS Bach vs Sinatra vs my neighbour that sometimes sing in his bath. Horrible logic, 0 guides.

  15. I try to sort a list alphabetically (asc) for Turkish app, but letters like ö,ç,ş,I,ğ,ü goes to end of the list. How can I fix it? Thank you

  16. how can i query data by the data text i made a video because its kinda hard for me to explain https://www.youtube.com/watch?v=BW7jWxNfeE0&t=61s

  17. It would be good if they could use the find() method as seen in MongoDB, but for some reason, even though Firebase is essentially a fork of MongoDB, it doesn't contain any such method.

  18. why its not working in realtime in searchview? Query text = productreff.orderBy("Name").whereEqualTo("Name", searchView.getQuery()); after this step, my app has crashed

  19. Hello, it seems there is an error in the code shown at 4m19s. It should be const events = db.ref().child('events'); instead of const events = db.child('events'); (or const events = db.ref('events');)

  20. So let's say I have a books database and I want to search a substring in the books titles. Not clear if it's even possible !

  21. It's a evil query sintax I cannot found a simple query like this : select * from events where eventname like '%MEET%' and de case sensitive is a other evil Heeeellllpppppp

Leave a Reply

Your email address will not be published. Required fields are marked *