0
0
GCPcloud~5 mins

Firestore document model in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
Firestore stores data in documents, which are like small records. These documents are grouped into collections. This model helps organize and access data easily without complex tables.
When you want to store user profiles with flexible fields that can change over time.
When you need to save chat messages grouped by conversation threads.
When you want to keep product information organized by categories.
When you want to quickly retrieve data without complex joins.
When you want to scale your app with a NoSQL database that handles lots of reads and writes.
Config File - firestore.rules
firestore.rules
rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}

This file sets basic security rules for Firestore. It allows all reads and writes for learning purposes. In real apps, you restrict access here.

rules_version: specifies the rules syntax version.

service cloud.firestore: targets Firestore database.

match /databases/{database}/documents: applies rules to all documents.

allow read, write: if true;: allows all operations unconditionally.

Commands
This command creates a Firestore document with ID 'user123' in the 'users' collection. It stores simple user data as fields.
Terminal
gcloud firestore documents create users/user123 --data '{"name":"Alice","age":30,"email":"alice@example.com"}'
Expected OutputExpected
Created document users/user123
--data - Specifies the JSON data fields for the document.
This command retrieves the document 'user123' from the 'users' collection to verify its contents.
Terminal
gcloud firestore documents get users/user123
Expected OutputExpected
{ "name": "Alice", "age": 30, "email": "alice@example.com" }
This command updates the 'age' field of the 'user123' document to 31, showing how to change data in a document.
Terminal
gcloud firestore documents update users/user123 --data '{"age":31}'
Expected OutputExpected
Updated document users/user123
--data - Specifies the fields to update in the document.
This command deletes the 'user123' document from the 'users' collection, cleaning up the data.
Terminal
gcloud firestore documents delete users/user123
Expected OutputExpected
Deleted document users/user123
Key Concept

If you remember nothing else from this pattern, remember: Firestore stores data as flexible documents inside collections, making it easy to organize and access data without tables.

Common Mistakes
Trying to store deeply nested data without using subcollections or flattening fields.
Firestore documents have size limits and complex nested data can cause performance issues.
Use subcollections for related nested data or flatten fields to keep documents small.
Using the same document ID for different data types in the same collection.
This can cause data overwrites and confusion when reading documents.
Use unique document IDs per data type or separate collections for different data.
Not setting Firestore security rules and leaving open access in production.
This exposes your data to anyone and risks data loss or leaks.
Write and test security rules that restrict access based on user identity and roles.
Summary
Create Firestore documents with JSON data fields inside collections.
Retrieve documents to check stored data.
Update specific fields in documents without overwriting all data.
Delete documents to remove data when no longer needed.