Firestore 数据建模文章和类别

the*_*ral 4 conventions data-modeling categories firebase google-cloud-firestore

语境:

我正在用 Angular 创建一个类似 wiki 的页面。wiki 页面的文章总数可能不会超过 5000 篇。我想获得最有效的(页面加载)方式,但我认为我对此太陌生,无法监督一种选择相对于另一种选择的后果。当然我也想遵守惯例。

问题:

我在 firestore 中有一系列文章,我想对其进行分类。一篇文章应该属于一个类别。一个类别可以属于一个类别(作为子类别)。

现在,哪种数据模型是首选?为什么?

  1. 每篇文章都有一个引用类别文档的属性(参考数据类型)?

  2. 类别文档是否包含一系列对文章的引用?

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

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

Run Code Online (Sandbox Code Playgroud)
  1. 完全不同的东西吗?

Ale*_*amo 6

  1. 每篇文章都有一个引用类别文档的属性(参考数据类型)?

仅当文章可以属于单个类别时,此结构才会对您有所帮助。

Firestore-root
   |
   --- articles (collection)
         |
         --- articleId (document)
               |
               --- catoegory: "Science" //Simple property
Run Code Online (Sandbox Code Playgroud)

无需使用对类别文档的引用。除此之外,要过滤您的文章,您可以简单地使用一个where equal电话。

  1. 类别文档是否包含一系列对文章的引用?

如果一篇文章可以属于一个或多个类别,则此结构将对您有所帮助。

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

现在要过滤您的文章,您只需使用 wherearray-contains调用即可。

  1. 完全不同的东西吗?

在构建 Cloud Firestore 数据库时,这两种解决方案都被广泛使用,但您应该选择哪一种更适合您的应用程序的用例。