Introduction to Google Cloud Firestore
Picking the right database solution on Google Cloud Platform can be tricky. There are so many options to choose from, and sometimes the differences between them are not very obvious. In this course, you will learn about Firestore, one of Google’s NoSQL databases. We will cover what it can do, how it differs from some other databases, and how to get started using it.
- Understand what Google Cloud Firestore is
- Learn how to use it to store and retrieve data
- Know how to create indexes
- Know how to install the emulators for local development
- Database administrators
- Cloud Architects
- GCP Developers
- Anyone preparing for a Google Cloud certification
- Some experience with databases and writing queries
So now it is time for a practical demonstration of how to set up and configure Firestore. I am going to show you how to add, edit and delete records, and how to write some basic queries. I will also cover importing and exporting records, as well as creating indexes. Now that’s a lot to cover, so let’s get started.
First things first, you are going to need to log into the web console. You can see that I have already done that. Next, search for “Firestore” and then click on the link. This will take you to the main Firestore page. And if this is your first time you will be prompted to choose between the Datastore mode or Native mode.
Picking a mode will lock your project to that selection. You cannot have a mix of both Datastore and Native mode databases in the same Google Project. So make sure you choose carefully. You can scroll down to view a summary of the differences. I don’t personally find this table super helpful, but it does show some of the tradeoffs. I think you will have a better understanding after you finish watching this video.
Now even though this message claims that the selection is permanent, it is actually possible to switch if your database is empty. So if you accidentally picked Datastore mode, but you really want to switch to Native mode, all is not lost. As long as you don’t have any records added, you can still change your mind.
To create a Datastore mode database, click this button and then choose a location. Your database can be created in a particular region, or you can actually make it multi-regional if you want. Multi-regional databases provide the highest availability and durability. However, single-region databases are cheaper and they have lower write latency. Once you make your selections, then just click on “Create Database” to confirm. Creating a Native-mode database is pretty much the same. Just click this button here and then pick your location like before.
Now because I want to show you both modes, I am going to need two different projects. So I am going to bring up a new window with two tabs. Each tab contains a separate project. The first tab contains my Datastore mode project. And the second tab contains my Native mode project. By flipping back and forth between these tabs, I can show you the differences between the two modes.
To view any Datastore mode databases, you have to search for “Datastore”. And to view any Native mode databases, you need to search for “Firestore”. Now, this is just one of the many confusing decisions that Google made. Datastore is actually Firestore just running in Datastore mode. You can see it actually says that here on the page. However, they make you access this page by searching for “Datastore” instead of “Firestore”. If you try to search for Firestore, it gives you a warning and makes you redirect to the correct page.
So it’s pretty confusing. Half of the time Google treats Datastore as if it is a separate product and not just a mode in Firestore. Anyways, just remember that when you are using Datastore mode, that you need to search for “Datastore”. If you are using Native mode, you can search for “Firestore”.
So you can see here that when you first create a Datastore mode DB, it is going to give you the option to convert it to Native mode. Now, this is only available if you don’t have any records created yet. Also, what is a little bit strange is this option is not available on the Native mode screen. However, there is a way that you can convert from Native mode to Datastore mode. It is a command you run in Cloud Shell or the SDK. Again, this only works if your database is completely empty.
Ok, now we have two databases created. Our next step is to start adding some records. In Datastore mode, records are called “entities”. To add a record, you click on the “Create Entity” button. In Native mode, records are instead called “documents” and documents are stored in “collections”. So to add a new record, you need to click on the “Start Collection” button.
So you will notice that the two screens for adding a record look pretty different. In Datastore mode, you are going to create things called entities. And entities are organized by namespace and kind. Namespaces allow you to partition your records into subsets. You can think of them as “buckets” for holding certain types of data. Maybe you need to split up your data by application or team or environment. Or you can just leave everything in the default namespace if you wish. Using multiple namespaces is optional, but it can be useful in certain situations.
Now entities are also organized into “kinds”. A kind determines what sort of record you are creating. So if you are storing information about a user, you would set the kind to “user”. Or if you are storing information about a video, then your kind would be “video”. Just use this field to describe whatever the entity represents.
In Native mode, you don’t have entities. Instead, you have “documents”. And documents are broken up into collections. So a document represents a record. And collections are used to organize and group your documents together.
Even though these two modes store data in very different ways, they can both be used to store the exact same data. Let’s come up with a real-world example. Let’s say that I am a book collector. And I want to create a database containing information on all the books that I currently own. To create a record in Datastore mode, I would probably leave the namespace set to default, and for kind, I would enter “Book”. That means each record will represent one of my books. And then I just need to add fields to hold the actual information. In Datastore mode, these are called “properties”. So I am going to add a property called “Title” to store the title of the book. Title should be a string, so I will leave that alone. And then let's say the first book I own is a copy of “The Hobbit”. Finally, I need to choose whether I should index this property or not.
Now indexing is what allows you to quickly search and sort by fields. So since I will probably need to be able to search by book title, then I need to leave this checked. In Datastore mode, there is an upper limit to the number of indexes that are allowed. So you only want to index the fields you actually need to.
Now let me show you how to do the same thing in Native mode. First I need to create a collection. So I am going to call this “books”. Then inside the collection, I am going to create a document. Now every document needs a unique ID. You can pick one yourself, but in most cases, I would recommend letting Google generate it for you.
Ok, now I need to add a field for the title. Again, it needs to be a string. And you should also notice that there is no option here for indexing. That is because in Native mode all fields are automatically indexed for you. You still have to manage indexing multiple fields, but we will look at that a little later.
Ok, so far in both databases, I just have a single record with a single field. Of course, I am going to want to store more than just the book title. So let me show you how to add more fields. In Datastore mode, you have to click on the “Add Property” button. Then pick a name for the new field, pick the type, and enter the value. So I am going to add a field for “Author”. I will also leave this indexed since I probably want to search by author. In Native mode, you just have to click on “Add Field”. Then enter the name, type, and value.
Next I want to store the publishing date. So let me add that. Datastore mode doesn’t have an option for just a date, so I will use a “date and time” field and then I can just ignore the time part. For this field, I am going to turn off indexing, because I don’t anticipate needing to search by publishing date.
Ok, let me add a few more fields. I am going to add a true/false flag to store if the book is a paperback. And I won’t need to index this field either. And then I want to store the International Standard Book Number or ISBN. Now this can either be a 10 digit number. Or there is an extended 13 character version as well. Some books will just have one of these, but others will have both. Now these I do want to index, because I might want to look up a particular version of a book to see if I own it already. Alright, that seems like enough fields.
I’ll go ahead and add the rest of the fields to my Native mode DB as well. I just have to enter the names, pick the most appropriate field types, and then paste in the values. Ok there we go.
So as you can see, the two modes have very different ways of storing and organizing your data. Let me go ahead and save my changes. I just need to click on “Create” in Datastore mode. And click on “Save” in Native mode.
Here we can see a difference between how the records look. In Datastore mode, you view entities by “kind”. Right now I am viewing all “Book” entities. I can change this dropdown to select any kind of record I want. This is how records look in Native mode. So here you get the list of collections here on the left. And then if you click on a collection, you can see the list of documents in the middle. And if you click on a particular document, you can view the details on the right. So Datastore records are basically organized into buckets. And Native mode records are basically organized into paths. You can see the path is made up of the collection and document ID.
Editing records is pretty intuitive. In Datastore mode you just click on the record you want to change and then make any edits you need. You can change values, types or even modify indexes. Native mode is pretty similar. To edit the fields you hover over them and then click on the pencil icon. Also, you can edit the path of a document by clicking here.
Deletion is just as simple. Just click on the menu here. Or in Datastore mode, click on the button here. Alright, so now you know how to add, edit and delete records.
Now this works great if you are going to be manually typing in all your data. However, this is rarely the case. So next I want to show you how to import and export records.
Daniel began his career as a Software Engineer, focusing mostly on web and mobile development. After twenty years of dealing with insufficient training and fragmented documentation, he decided to use his extensive experience to help the next generation of engineers.
Daniel has spent his most recent years designing and running technical classes for both Amazon and Microsoft. Today at Cloud Academy, he is working on building out an extensive Google Cloud training library.
When he isn’t working or tinkering in his home lab, Daniel enjoys BBQing, target shooting, and watching classic movies.