Firestore datamodelling articles and categories

回眸只為那壹抹淺笑 提交于 2020-06-16 12:00:42

问题


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?

回答1:


  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.



来源:https://stackoverflow.com/questions/56239419/firestore-datamodelling-articles-and-categories

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!