What are Firestore subcollections?
Answer
Subcollections are collections nested inside documents, enabling hierarchical data structures. A document can have any number of subcollections, each with their own documents, and those documents can have their own subcollections (up to 100 levels deep). Example: /users/{userId}/orders/{orderId} — each user document has an orders subcollection. Benefits: (1) Logical grouping — orders are naturally associated with their user; (2) Query isolation — you can query all orders for a specific user efficiently; (3) No size impact — subcollection data doesn't affect the parent document's 1MB limit; (4) Security — subcollection access can be controlled independently of the parent. Caution: you cannot query across subcollections with a standard collection query. If you need to query all orders across all users, use a top-level collection instead of subcollections. Collection Group Queries (introduced 2019) allow querying all subcollections with the same name across all documents.