What is Firestore?
Firestore is a horizontally scaling NoSQL document cloud database provided by Google as part of Firebase (Firebase Realtime Database is another database offering by Firebase). Firestore keeps the data in sync across client applications through realtime listeners and offers offline support for mobile and web.
Watch this great intro video:
Note that in GCP you can only have one Firesotre instance per project.
How does data store in Firestore?
Firestore supports hierarchical data structure where data stored in documents organized into collections. Each collection in Firestore contains a list of documents representing a state of your data (a dictionary of key/value pairs). Each document itself can contain a list of sub-collections which enables Firestore to store complex nested objects.
Unlike MongoDB, where data stores in Binary JSON format (Bason), data stores in key pair values. By using any client SDK, you can serialize that to you object model.
- Collections must only contain documents, and nothing else
- Documents must be less that 1MB in size (that excluding the collections in the document)
- Documents can’t contain another documents (but they can contain sub-collections)
- The Firestore root can only contain a list of collections
Consider a simple blog data structure. It can be represented as below:
This data model will be represented as a collection of blogs. Each blog has reviews sub-collection. Users store in another collection and a copy of user’s name will be presented in a blog post or blog’s reviews.
Accessing Firstore cloud
You can view Firestore in GCP console. The top level blog collection looks like this:
A blog document with reviews sub-collection. Note review document hierarchy in the breadcrumb.
The review document path is:
How to query data in Firestore?
Having this structure you can query a document by either referencing each document inside a collection:
Or by using the document’s path (which consists of collections and documents):
Note that when you query for documents, Firestore performs a shallow query, that is, it won’t return any nested sub-collections they contain, it only returns the documents.
Next we will learn how to create a project in C# to work with firestore.