Firebase Fundamentals

Firebase Fundamentals Map & Querying Arrays

Learning Paths:
Topics:
Check out a free preview of the full Firebase Fundamentals course:
The "Map & Querying Arrays" Lesson is part of the full, Firebase Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

David walks through querying through arrays with array operators such as array-contains and array-contains-any. A more complicated example of querying a date range with set excluded dates is also demonstrated in this segment.

Get Unlimited Access Now

Transcript from the "Map & Querying Arrays" Lesson

[00:00:00]
>> Array queries. So arrays are a very interesting type in Firestore, because you can query on them and there's a lot of power when you're doing array queries. So when people started initially modelling their data in Firestore, they might use an object or a map to query their documents.

[00:00:17] So maybe let's say you wanted to, a new requirement comes down and expenses now should be able to be tagged by multiple categories. So not just one category, it could be food or food and transportation, right? Maybe it's like a party bus, I don't know. But you have multiple tagging for a single expense.

[00:00:39] So maybe you have it as a categories object and then you put the name of the category. But then you're writing something weird where it's like where categories.fun = true. And it works, but it's weird, and this is what your document would have a categories object that looks something like that.

[00:00:56] But instead, we could just store it as an array, and so it has a list of categories. And then we can use special array operators. So this is array contains. So it says hey, get expenses where the category contains fun, so it can have another category to it, but it has to contain fun.

[00:01:21] And then we also have array-contains-any, which is more of your or style operations. Where you can say hey, give me categories where any of the fields within array or any of the elements within the array of categories is fun or kids. And so this is, to answer the question earlier, this is one of the places where you can do an or style query and limited to 25.

[00:01:46] So this is in the querying arrays page in the same project, just in this in pages/QueryingArrays. And let's write this expense right here. So get the first 100 categories that are categorized as fun and kids. So now we're no longer dealing with single categories, multiple categories. So expensesCol, where categories, where it equals this because it must equal exactly that.

[00:02:23] With array contains, it's not an array one, and I always get that confused cuz the array contains any is all the multiple or values. Array-contains is single values, whereas you can actually this is where the syntax, this is what, array-contains looks like this. And so array-contains is usually used for one value because now it's saying the array must contain something that looks like that.

[00:02:51] Okay, so on equals we have that, so now we're gonna do the second one, get the first 25 categories that are categorized as ONLY fun OR kids. So within this I can say, expensesCol and where categories, And array-contains-any, fun, kids. Limit 25. All right, so now the only thing I meant to say as only it's just fun or kids.

[00:03:50] And so within here we have fun, taxes, kids, utilities, kids, healthcare, fun, houses. But no matter what, it's going to be fun and kids is just going to be the results, yeah. And so this one is fun, just single one of fun. So now, I can query based upon multiple or values.

[00:04:19] And so this is an array-contains one where it says give me the first ten categories that contain the fun category. So where, categories, array-contains, fun. And so right here, it's fun, taxes, fun, housing, each field is guaranteed to have fun. I like this query. This one right here is more advanced.

[00:04:50] It says, get the first 25 expenses that occurred in January, 2021, but not on 12/30, 12/26, 12/23, 12/28. So this is just like the one we did in the previous one, but it has more restrictions on it. So getting to that, we can just copy this over for the sake of time.

[00:05:17] And so get the first, so you can get rid of food for this, get the first 25. So limit 25 that occurred within January, but not on. So in this case, instead of not equals, I can say not-in, and then this has to all be date. So what I'm gonna do here is just format it out like this, so we have 12/26, 12/30.

[00:05:54] 12/23 and 12/28. So now when I refresh, let's look for those dates. I only needed the first 25 and I think there's more than that, so up until like 250. Yeah, so we have all these dates, still, more, wow! This is big. Let's do 350. Man, this is still, this is a huge collection, wow, 450.

[00:06:36] All right, that's more like it. So here, we don't have 12/26, 30, 23, 28. So 29 to 27, so we're skipping out of 28. Same here, when we get to 24, we don't have 23. We go straight to 22 and then we also skipped over 26. So now we've limited out specific dates.

[00:06:58] So that is querying with both, not in, in, with querying with array values for arrays. It's a really powerful way of getting more out of your query. So if you're structuring your data in Firestore by saying, hey, let me store this as an array, there's a lot more querying power that you're gonna get out of it.

[00:07:18] But one thing that I think was serendipitous that happened was. You notice how to keep increasing this limit to get more, and more, and more? Well, there's gonna be a lot of times in your queries where you want to continue the range, where you say, hey, I got the first page, so to speak.

[00:07:35] So I wanna get the second page or the third page. On FireStore we have a whole set of operators that we call cursors, that allow us to not only increase the window. Because you can feasibly see how every time you click the button, you could just increase the limit.

[00:07:51] But then you'd go from reading 0 to 100 to 0 to 450. And you're just increasing this range that might not be needed for the page, cuz you're only ever showing maybe a hundred results at a time. So what we're gonna look into doing next is called cursoring.

[00:08:07] Which is keeping an active window of a hundred or whatever end amount of documents you want in a query from one section on to the next.
>> Is it possible to query where category equals fun or food or where an author equals David?
>> Yes, that right there, you could do, based on a composite query because you have categories as one field.

[00:08:34] And then author name would be another field. And so within those, you would have to create a composite query for production, but yes, you can do that.