Firebase Fundamentals Collection Group Queries Exercise
Transcript from the "Collection Group Queries Exercise" Lesson
>> So I'm gonna show off how collection group queries work. All right, collection group queries, that is in this colour collection group queries. So I wanna get the first 100 expenses across all users that are under $100. So what I've done in the emulator is if we go to users, each user does have an expensive sub collection.
[00:00:30] I've sort of preloaded all of this with everything we could possibly need. So this user actually does have all of their expenses loaded within here. So within here, I can say, okay, well, let me start with the expenses column and what I want to do actually instead of expenses column I want to do a collection group.
[00:00:55] So what I'm gonna say is create a, Expenses group that is a collection group. No not capital c, being imported, okay, yeah, it's being imported. Collection group of FireStore, it's passing in FireStore first and then passing in R ID of expenses. So I'll say expenses group and I want to limit first 100 and I want to say where cost is less than 100 So now, this really does the same thing as we had before, but allows us to have flexibility.
[00:01:51] So I can get by a specific user or I can get within, without running a query or I can get everything by running a collection group query. And so by structuring my data this way, it's almost like I have two collections of all expenses and individual expenses. And when you start to think about your data being structured that way and the no SQL world.
[00:02:12] You'll find that a lot of your queries become really based upon where they are located, rather than all the constraints that you need. And that gives you extra fields and extra things to query on. Because the goal is in the know SQL world is to make sure that all those reads are as fast as possible.
[00:02:32] So the second one, Is I wanna get the first 100 expenses across all users from any date range, I think that you can make up whatever you want. So to do that it's, again, expenses group and I'm going to say, where first 100 expenses, so limit 100. And I'll say where date is equal to new date 12/15/2021.
[00:03:16] And just like that, all the expenses, so, collection group queries are super powerful and that way it gives you way more flexibility. Just make sure that you take advantage of the hierarchy that FireStore provides you and you can do a lot of amazing things.