Ad

Firestore Datamodelling Articles And Categories

Context:

I am creating a kind-of-wiki page in Angular. The wiki page would probably not get bigger than 5000 articles in total. I want to get the most efficient (pageload) way possible but I think I am too new to this to oversee the consequences of one option over the other. Of course I also would like to follow conventions.

Problem:

I have a collection of articles in firestore which I want to categorize. An article should belong to one category. A category can belong to one category (as a sub category).

Now, Which data-model is preferred? And why?

  1. Every article having an attribute (of reference datatype) referring to the category document?

  2. The category documents having an array of references to the articles?

Firestore-root
   |
   --- categories (collection)
         |
         --- categoryId (document)
               |
               --- name: "Science" //Simple property
               |

               --- articles ['articleId1', 'articleId2', etc.. (long array)]

  1. Something completely different?
Ad

Answer

  1. Every article having an attribute (of reference datatype) referring to the category document?

This structure will help you only if the an article can belong to a single category.

Firestore-root
   |
   --- articles (collection)
         |
         --- articleId (document)
               |
               --- catoegory: "Science" //Simple property

There is no need to use a reference to a category document. Beside that, to filter your articles, you can simple use a where equal call.

  1. The category documents having an array of references to the articles?

This structure will help you if the an article can belong to one or more categories.

Firestore-root
   |
   --- articles (collection)
         |
         --- articleId (document)
               |
               --- catoegories: ["Science", "Economy"] //Property of type array

Now to filter your articles, you can simply use a where array-contains call.

  1. Something completely different?

Both solutions are widely used when structuring a Cloud Firestore database, but you should choose which one is more appropriate to the use-case of your app.

Ad
source: stackoverflow.com
Ad