0
0
Firebasecloud~5 mins

Document-collection data model in Firebase - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you store data in Firebase Firestore, you organize it in documents and collections. This helps keep your data neat and easy to find, like folders and files on your computer.
When you want to save user profiles with different details for each user.
When you need to store messages in a chat app, grouped by conversation.
When you want to keep track of products in an online store, each with its own info.
When you want to organize blog posts and their comments separately.
When you want to quickly find data by grouping related items together.
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 simple rules allowing anyone to read and write data in Firestore. It is used here to let you test the document-collection model without permission issues. In real apps, you should secure your data with proper rules.

Commands
This command creates a document named 'user123' inside the 'users' collection with fields 'name' and 'age'. It shows how to add a single document.
Terminal
firebase firestore:documents:create users/user123 '{"name": "Alice", "age": 30}'
Expected OutputExpected
Document users/user123 created successfully.
This command retrieves the document 'user123' from the 'users' collection to verify it was saved correctly.
Terminal
firebase firestore:documents:get users/user123
Expected OutputExpected
{ "name": "Alice", "age": 30 }
This command creates a subcollection 'orders' under the document 'user123' and adds a document 'order001' with order details. It shows nested collections.
Terminal
firebase firestore:documents:create users/user123/orders/order001 '{"item": "book", "price": 15}'
Expected OutputExpected
Document users/user123/orders/order001 created successfully.
This command retrieves the order document to confirm the nested data was saved.
Terminal
firebase firestore:documents:get users/user123/orders/order001
Expected OutputExpected
{ "item": "book", "price": 15 }
Key Concept

If you remember nothing else from this pattern, remember: Firestore stores data in documents inside collections, and documents can have nested collections for organized, flexible data.

Common Mistakes
Trying to create a document without specifying the full path including collection and document IDs.
Firestore requires a collection and document ID to know where to store the data.
Always specify the full path like 'collection/document' when creating or accessing documents.
Storing large amounts of data in a single document instead of using collections and subcollections.
Documents have size limits and can become slow to read or update if too large.
Split data into multiple documents inside collections to keep each document small and fast.
Summary
Use collections to group related documents, like folders holding files.
Create documents with unique IDs inside collections to store data records.
Documents can have subcollections for nested, organized data.
Use commands to create and retrieve documents to manage your Firestore data.