Explain Firestore's data model with documents and collections.

Beginner

Answer

Firestore uses a hierarchical data model:

Documents:

  • Similar to JSON objects
  • Contain fields with values
  • Have unique IDs within collections
  • Can contain subcollections

Collections:

  • Containers for documents
  • Cannot contain other collections directly
  • Automatically created when first document is added

Structure Example:

users (collection)
  ├── user1 (document)
  │   ├── name: "John Doe"
  │   ├── email: "john@example.com"
  │   └── posts (subcollection)
  │       └── post1 (document)
  │           ├── title: "My First Post"
  │           └── content: "Hello World"
  └── user2 (document)
      ├── name: "Jane Smith"
      └── email: "jane@example.com"