Firebase with React, v2 Firebase with React, v2

Ordering, Limiting, & Querying

Check out a free preview of the full Firebase with React, v2 course:
The "Ordering, Limiting, & Querying" Lesson is part of the full, Firebase with React, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Steve gives examples of how to perform ordering, limiting, and querying on the Cloud Firestore.

Get Unlimited Access Now

Transcript from the "Ordering, Limiting, & Querying" Lesson

[00:00:00]
>> Steve Kinney: The other nice thing about Cloud Firestore is it supports some, let's call them SQLite-like queries, SQL-esque queries, if you will, right? So we can go ahead and get them in a particular order. And you might be like, for a NoSQL database, it's a giant tree, how does it do this?

[00:00:21] Well, it turns out for pretty much every single property in your database, Cloud Firestore under the hood has created an index. So you can pretty much sort, or filter, or limit by any property in your NoSQL document. I think up to 200 properties or something along those lines, right?

[00:00:42] At which point, you can just split them out to further documents and get more, but at a certain point, it will give up, but 200 is a lot. So you can order it, and basically all it does is creates these indexes and it'll start at a certain point and keep going.

[00:00:56] And the really interesting part is you could have 60 million documents in your database. It's not going to have to sort all 60 million because it already has the index in there. It'll just start at the top and it will go for however many you want and you only pay for however many you request.

[00:01:10] So, you having 60 million, 100 million documents in there is not upping your bill, unless you are fetching 100 million at a time. So, it gives you the ability to scale and have your application grow really big. While also being incredibly manageable if you write your code, in a way that supports that, right?

[00:01:29] So if you fetch all 100 million, yes, you're going to pay for that, but if you limit it, and we'll see that in a second, to ten and paginate. You will pay for ten at a time, no matter how big your database gets, which is really cool. In the real time database, you'd get all of the tree for something, right?

[00:01:44] And so it was a little trickier, on top of ordering, we can also limit, as I said before. So this will just get the first ten, and the other nice one that we can do is we can do querying now, right? Before you would just basically, like I said, you would get that part of the tree, you could sort a client-side.

[00:02:04] You could do all this stuff client-side, but you would have to basically structure your data in a way that worked for you. In this case we can say, where stars are greater than or equal to 10. You can also, you can't do substring searching. But you can theoretically do like alphabetical order, or something along those lines.

[00:02:21] It's basically those indexes are basically sorting your data. And then allowing you start from a given point and read up until given a point, right? So you kind of construct these queries and get SQL-like queries out of a NoSQL database, which is pretty cool. So that concludes the theory about the Cloud Datastore, Cloud Firestore rather, part of the show.

[00:02:48] Let's actually, let's start writing some code, all right?