Skip to content

Cloud Firestore Data Model

Reference: https://firebase.google.com/docs/firestore/data-model

Cloud Firestore is a NoSQL, document-oriented database.
Unlike a SQL database, there are no tables or rows.

Instead, you store data in documents, which are organized into collections. All documents must be stored in collections. Documents can further have sub-collections.
So the hierarchy looks something like -

'top-level collection' / 'document' / 'sub-collection' / 'sub-document' / ...

The rules for this hierarchy are:

  • collection and document are always alternating.
    • So, you cannot have a collection under a collection
    • and cannot have a document under a document.
  • Top-level is always a collection; cannot be a document.
  • Collection names as plural.

These rules are similar to Resource-Oriented Design where resources are similar to documents in Firestore, and the resource instances are grouped under a collection.

Then the Firestore path is similar to a strict version of REST API path.

firestore4k lets you construct the path (which can be root collection, document, sub-collection or sub-documents), and then perform corresponding operations (for collection or for document) in dynamic (flexible) of typed (type-safe) manner.

For the sample code in Guides, I will use:

  • a root (top-level) collection: users
  • its sub (child) collection: messages
Path Description
users users as root collection
users/user1 user1 document under users root collection
users/user1/messages messages sub-collection under user1 document
users/user1/messages/message1 message1 document under messages sub-collection