Check out a free preview of the full Firebase Fundamentals course
The "Simple Queries" Lesson is part of the full, Firebase Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:
David live codes examples of simple queries, including limited queries, queries for specific categories, less than queries, and queries for data within a set date range.
Transcript from the "Simple Queries" Lesson
[00:00:00]
>> And so, those are simple queries, as I see it's simple, just a bunch of little operators. So I'm gonna go through now using that same project that you have set up, you don't have to NPM install or do anything else. We're gonna go back to this foundational querying section, and I'm gonna write some queries and then you are gonna write some queries.
[00:00:18]
And then then I'll write some more queries in your query, so that's where we're at. All right, so in this project we are at this page called Real Time streams, we're gonna move on to foundational queries. And so just to make this simple, I have all these commented out ones we're going to just gonna focus on the query.
[00:00:38]
And then that query kind of gets, it goes straight to that bind to table function that we worked on in the last time. So we wanna get the first 200 expenses, well the first thing we do is wanna make sure that we have an expenses collection. So we have that written here, and then from here, we pass that in and we wanna now start restricting.
[00:01:02]
So we wanted to restrict, so we use the operator limit, and we limit to the first 200. So now I'll save and this is running right, this is, no one has it, probably no user. Yeah, one second, I think we just rerun everything. This was sitting for a very long time and bad things happen when things sit for a very long time.
[00:01:26]
All right, so I'm just doing NPM run dev to boot up the emulators and the dev server. Let's see here and we are good. So I'm gonna go to fundamental queering, and here we go, we have UID cost category from cache. It was our first read so it's false, and we can see that we just have, it's about 200 expenses.
[00:01:59]
And it's kinda hard to see if it was 200 just from all that scroll, so if it is limited to 20 I want to do a refresh, sorry. We can see that our from cache is true, which is interesting to see here because not only did I change the query entirely.
[00:02:17]
But I still served from the cache because as fire stores database, the underlying cache was able to say, I've seen these documents before. They haven't received any updates, no need to go out and refetch all this stuff, it tries to be as efficient as possible. There are still some cases where it might believe for some reason something's invalid, so we'll do a fetch or something behind the scenes.
[00:02:41]
So it does try to do its best though to make sure that it's only reading data in real time when updates are occurring. So that's a very simple query, but now let's get all expenses that are categorized as pets. So I'm gonna comment this one out again, so each expense so we always start by saying expenses call so what collection we're carrying on.
[00:03:08]
And then we're going to say where category is equal equal to pets. And I don't wanna actually get all even though I wrote it all here, I don't know what I was thinking, it's limit to 20 because it's easier to read through it. Saving when to go refresh I disabled the HMR on this because I don't know I feel like weird things happen when you're doing real time queries with HMR it's too much that's updating and it's hard to tell.
[00:03:37]
So I like to do a nice hard refresh to see what's new. So right here, we have 20 and they're all categorized as pets. Now, we're gonna do something a little bit more intensive. We're gonna get all expenses less than $200 ordered from greatest to least. So again, expenses column where costs, did I say greater than 200 or expenses less than 200.
[00:04:16]
And we're going to do an order by cost and then from greatest to least. So that is going to be in descending value, so the greatest are on top and the less or the least ones are in the bottom. And if you're anything like me you forget it ascending and descending at least once every three to four months and have to write a query like that's how that works again.
[00:04:43]
So now let's refresh, and then we have 199.93 because we didn't say equal to and then as we scroll down. Did I limit? What did I? I didn't do any limiting. So we can see these just go forever and let's see how far this goes down so all this I put a ton of expenses in here, goes all the way down to our cheapest expense $1.06.
[00:05:06]
So we read a ton of data. Let's comment that out. So now we wanna get number good dates, dates are in JavaScript always not fun to work with, right? Like that's why we have like six different date libraries that we always reach for every time someone asks us to format date though, the browser API has gotten a lot better at that.
[00:05:33]
So I don't wanna throw shade at that JavaScript dates and formatting them has gotten a lot easier. But with Fire Store querying by dates is actually really easy because Fire Store itself will work with a native JavaScript date object. So you don't have to do any formulation or any kind of weird formatting.
[00:05:49]
So I could say expenses call where and date and what was that again get all expenses that occurred before July of 2020. So I can say it's less than it's a new date, and then I just insert a date, so 6/1/2021. And I am gonna limit this one so limit to 20.
[00:06:19]
So let's save and refresh, and let's say we have ourselves this is May. There's a ton of expensive, so these all go in May, so we don't have any ones that are from June, and we can even do a range. So let's say I wanna get all queries, how many do I have left here?
[00:06:42]
Let's see. All right, I'm gonna do these two and then I'm go ask you all to do seven and eight, so let's see here. So we're gonna get all expenses between July of 2021 and October of 2021. So like always specify the collection that we're querying against and then we want to know where date is.
[00:07:10]
So to be between, we'll have to say is less than. Let's see here right we make sure so it's October 21, June, July, August, October. So less than a new date of 10/1/2021 but greater than, let's say, six, what are the dates or days in July 6/31? There's smarter ways to do this, but we'll just go with that for now.
[00:07:47]
Save and let me limit again limit to we'll do a little more this time, limit to 100. All right, cool, and so this is June and going down we have a ton of expenses so it only gets us to 07/09. But it's all composite queries. So actually, all right.
[00:08:11]
I'm gonna wait for the exercise to do on composite queries because composite queries I think are the real flex some of the muscles here. So those are simple queries as you can see the entire time. What made them simple, is that even though we have multiple ranges here, so greater than or less than date, it's still just on date.
[00:08:30]
And the same thing date here, cost, category, these are all simple queries because it's one field
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops