I'm struggling with Firestore. The question is how to structure data, without running into a billing trap / nightmare.
I have following data structure:
school
course1
section1
page1
page2
section2
page1
page2
...
I assume that a course usually would not have more than 50 sections.
Use a collection
So I could use a collection and create a document for each section that contains the name and description of each section.
db.collection("schools")
.document("school1")
.collection("courses")
.document("course1")
.collection("sections").snapshots()
Document Structure:
name: "Section 1"
description: "Description 1"
However if I need to display a list of the sections, then according to Firestore billing, I would get charged for each read of a document. Which means if there are 20 sections, I would get charged for 20 reads.
Use a document with nested collections
I could also just create a document "Course 1" and nest all the sections.
db.collection("schools")
.document("school1")
.collection("courses")
.document("course1")
.get()
Document Structure:
name: "Course 1"
description: "The description",
sections: [
{
name: "Section 1",
description: "Description 1"
pages: [
{name: "Page 1", description: "Page Description 1"},
{name: "Page 2", description: "Page Description 2"}
]
},
{
name: "Section 2",
description: "Description 2"},
pages: [
{name: "Page 1", description: "Page Description 1"},
{name: "Page 2", description: "Page Description 2"}
]
...
]
Then I would only get charged for 1 read. Most likely I would not run into the 40'000 attributes limit and also not into the 1 MB limit.
But it looks like it takes some time to load the data from the document using FutureBuilder, which seems to be faster, if I get the documents in the collection with StreamBuilder.
So I somehow can't decide, which approach to take. It would somehow be more logical to use a collection because I will never run in any limits, and it seems faster to load, but from a billing point of view, it would make more sense, to nest the sections.
What option is the better one?