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?
Every article having an attribute (of reference datatype) referring to the category document?
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)]
- Something completely different?
Answer
- 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.
- 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.
- 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.
Related Questions
- → How can I query Firebase for an equalTo boolean parameter?
- → How can I access nested data in Firebase with React?
- → Firebase simple blog (confused with security rules)
- → Removing item in Firebase with React, re-render returns item undefined
- → AngularJS Unknown Provider Error (Firebase & AngularFire)
- → How do you pass top level component state down to Routes using react-router?
- → "this" is null in firebase query function in reactjs
- → Angular Module Failed to Load
- → Multiple dex files define Lcom/google/android/gms/internal/zzrx;
- → Joining Firebase tables in React
- → How can I make add firepad to my reactjs project?
- → How to use Cloud Functions for Firebase to prerender pages for SEO?
- → React.js component has null state?