Firestore Database is a product from Google's Firebase. It's a flexible, scalable, NoSQL cloud database. It allows you to store your app data and uses real-time listeners to keep the data in sync. It works well in all situations regardless of your internet connectivity.
Firestore Database offers the following features:
- Flexibility: It allows you to store your data in documents that are organized into collections. This approach allows you to have multiple documents (inside the same collection) that might not have the same structure.
- Querying: You can form a query to get specific documents or to retrieve all the documents in a collection.
- Realtime: Firestore database helps you achieve data synchronization to update data on any connected device.
- Offline: You can write to the database even when offline. When the app is not connected to the internet, the data gets stored in the local device, and when the device comes back online, the data gets synced to the cloud Firestore database.
- Scaling: Firestore database runs on Google's infrastructure. It provides automatic multi-region data replication, strong consistency guarantees, atomic batch operations, etc.
Let's understand the Firestore database (NoSQL Database) more in detail.
The NoSQL database is a schema-less database. That means the data is NOT stored in the table format. You actually don't have any restriction on how you store your data. The Firestore database uses the collection and document model to store the data.
Here are some important terms you should know:
- Collection: A Collection is simply a set of Documents.
- Document: A Document is a record that contains the Fields. The names of documents within a collection are unique.
- Fields: The key-value pair inside the document are called Fields. Field can contain the value for various data types such as strings, numbers, boolean, and so on.
To better understand, see the figure below:
Collection document model
Each user data is stored in a document and such multiple documents are organized into a collection. The documents inside the collection may or may not have the same fields. This allows you to introduce the new field for the new document without having to worry about adding the field for older documents.
To understand how to structure the database, consider an example that allows users to comment on a post.
Firestore Database allows you to structure the Database in the following ways:
In Top-level collections, multiple collections are created at the root level of your database.
For example, you create collections such as comments and posts at the root level. Comments for all the posts are stored in a single top-level collection and you also add the additional reference fields that uniquely identify the post.
Pro tip: Top-level collections are best when you have several queries/filters or search on a collection that is not based on the other collection. For example, loading or searching by all comments and not just on the comments of any specific post (i.e show all comments that have more likes, show all comments by specific order, etc.)
Collections are created inside the document. Such a collection is called subcollection.
For example, you create the top-level collection such as posts and then create a comments collection (as a subcollection) inside the posts collection. With subcollection, there is no need to add the reference fields to identify the post.
Pro tip: Subcollection is best when you have several queries/filters or search on a collection that is based on the other collection. For example, loading or searching the comments of a specific post. (i.e show all comments of a specific post that have more likes, show all comments of a specific post by specific order, etc.)
Here are some rules to create a Firestore database:
- Collection can only contain Documents.
- Your Document should be less than 1MB in size.
- The root of your Firestore database only contains collections.
MongoDB, Cassandra, and ElasticSearch are the other No-SQL database solutions that exist in the market.