Designing a chat app with multiple rooms and persistent message history using Firestore. How should you model rooms and messages?
Choose an answer
Tap an option to check your answer.
Correct answer: Create a rooms collection; each room document contains a subcollection whose documents each hold a single message..
Why this is the answer
This approach leverages Firestore's subcollections, which are ideal for handling collections of related documents that are tied to a parent document. Each room document acts as a logical container for its messages. This allows for efficient querying of messages within a specific room and scales well as the number of messages per room grows. Storing the entire message contents within the room document (Option 1) would lead to very large documents, making reads inefficient and potentially hitting document size limits. Creating a subcollection with one document per message for each room (Option 2) is essentially the same as the correct answer, just phrased slightly differently. Using a separate top-level messages collection (Option 4) would require complex and less efficient queries to retrieve all messages for a specific room, as you'd need to filter the entire messages collection.
Pass your exam — without the endless answer hunt
Get every verified question and explanation for this exam in one place, and save hours of prep. 1,000+ certifications · 20+ languages · free to start.
Pass your exam faster → No card needed