Design a Firestore schema to support an unknown number of articles and comments where users can post and view comments. How should comments be stored?
Choose an answer
Tap an option to check your answer.
Correct answer: Store each comment in a subcollection of the article..
Why this is the answer
Storing each comment in a subcollection of the article is the most scalable and efficient solution for an unknown number of comments. Subcollections allow for virtually unlimited comments per article without impacting document size limits. This structure also naturally groups comments with their respective articles, simplifying queries for all comments on a specific article. Adding comments to an array property on the article is problematic because Firestore documents have a 1MB size limit, which can quickly be exceeded with many comments, especially if comments are long. Storing comments as separate documents and adding their keys to an array property on the article still faces the 1MB document size limit for the array of keys. Storing comment keys on the user profile would make retrieving all comments for a specific article very inefficient, requiring multiple queries and complex data aggregation.
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