0
0
GCPcloud~5 mins

Firestore collections and documents in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
Firestore is a database that stores data in collections and documents. Collections are like folders, and documents are like files inside those folders. This helps organize and find data easily.
When you want to save user profiles for a mobile app.
When you need to store messages in a chat application.
When you want to keep track of products in an online store.
When you want to organize blog posts and their comments.
When you need a flexible database that grows with your app.
Commands
This command creates a Firestore database in the us-central1 region. You need a database before adding collections and documents.
Terminal
gcloud firestore databases create --region=us-central1
Expected OutputExpected
Created Firestore database (default) in region us-central1.
--region - Specifies the location for the Firestore database.
This command creates a document with ID 'user123' inside the 'users' collection. The document stores a name and age.
Terminal
gcloud firestore documents create users/user123 --data='{"name":"Alice","age":30}'
Expected OutputExpected
Created document users/user123.
--data - Provides the JSON data to store in the document.
This command retrieves the document with ID 'user123' from the 'users' collection to check its contents.
Terminal
gcloud firestore documents get users/user123
Expected OutputExpected
{ "name": "Alice", "age": 30 }
This command lists all documents inside the 'users' collection to see what user records exist.
Terminal
gcloud firestore documents list users
Expected OutputExpected
users/user123
Key Concept

If you remember nothing else from this pattern, remember: Firestore organizes data in collections (like folders) and documents (like files) to keep data neat and easy to find.

Common Mistakes
Trying to create a document before creating the Firestore database.
Firestore needs a database to exist before you can add collections or documents.
Always create the Firestore database first using the create command.
Using incorrect JSON format in the --data flag when creating a document.
Invalid JSON causes the command to fail and the document won't be created.
Use proper JSON syntax with double quotes and no trailing commas.
Confusing collection names and document IDs in commands.
Commands require the correct path format: collection/document. Mixing them causes errors.
Always specify collection first, then document ID, separated by a slash.
Summary
Create a Firestore database in a chosen region before adding data.
Add documents inside collections using JSON data to store information.
Retrieve and list documents to verify and manage stored data.