0
0
FirebaseHow-ToBeginner · 4 min read

How to Structure Data in Firestore: Best Practices and Examples

In Firestore, data is structured using collections that contain documents, which can hold fields and nested subcollections. Organize data by grouping related items in collections and use documents as individual records to keep your database scalable and easy to query.
📐

Syntax

Firestore organizes data in a hierarchy of collections and documents. A collection is a group of documents, and each document contains fields with data or nested subcollections. Documents are identified by unique IDs within their collection.

Example parts:

  • collection('users'): Accesses the 'users' collection.
  • doc('userID'): Accesses a specific document by ID.
  • set(data): Writes data to a document.
  • collection('orders'): A subcollection inside a document.
javascript
const db = firebase.firestore();

// Access a collection
const usersCollection = db.collection('users');

// Access a document inside the collection
const userDoc = usersCollection.doc('user123');

// Set data in the document
userDoc.set({
  name: 'Alice',
  age: 30
});

// Access a subcollection inside the document
const ordersSubcollection = userDoc.collection('orders');
💻

Example

This example shows how to create a users collection with documents for each user, and inside each user document, a orders subcollection to store their orders.

javascript
const db = firebase.firestore();

// Add a user document
const userRef = db.collection('users').doc('user123');
userRef.set({
  name: 'Alice',
  email: 'alice@example.com'
});

// Add an order in the user's orders subcollection
const orderRef = userRef.collection('orders').doc('order001');
orderRef.set({
  product: 'Book',
  price: 15.99,
  status: 'shipped'
});
Output
Data written to 'users/user123' and 'users/user123/orders/order001' documents.
⚠️

Common Pitfalls

Common mistakes when structuring Firestore data include:

  • Storing too much data in a single document, which can slow down reads and writes.
  • Using deeply nested subcollections unnecessarily, making queries complex.
  • Not planning document IDs and collection names, leading to confusing or inconsistent data paths.
  • Trying to perform complex joins, which Firestore does not support natively.

Keep documents small and use subcollections only when logically grouping related data.

javascript
/* Wrong: Storing many orders inside one user document as an array (bad for scaling) */
const badUserDoc = db.collection('users').doc('user123');
badUserDoc.set({
  name: 'Alice',
  orders: [
    { product: 'Book', price: 15.99 },
    { product: 'Pen', price: 1.99 }
  ]
});

/* Right: Use a subcollection for orders */
const userRef = db.collection('users').doc('user123');
userRef.collection('orders').doc('order001').set({
  product: 'Book',
  price: 15.99
});
📊

Quick Reference

ConceptDescriptionExample
CollectionGroup of documentsdb.collection('users')
DocumentIndividual record with fieldsdb.collection('users').doc('user123')
FieldKey-value data inside a document{ name: 'Alice', age: 30 }
SubcollectionNested collection inside a documentuserDoc.collection('orders')
Document IDUnique identifier for a document'user123', 'order001'

Key Takeaways

Structure Firestore data using collections and documents for clear organization.
Use subcollections to group related data without overloading single documents.
Keep documents small to ensure fast reads and writes.
Plan your document IDs and collection names for easy data access.
Avoid deeply nested data and complex joins; Firestore is optimized for simple hierarchical data.