在 Firestore 中使用根集合与子集合相比有什么好处? [英] What are the benefits of using a root collection in Firestore vs. a subcollection?

查看:19
本文介绍了在 Firestore 中使用根集合与子集合相比有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着集合组查询的出现,我不清楚使用根集合有什么好处.在 Firestore 团队的这篇文章中,唯一的事情我可以看到的是,存在名称冲突的可能性,安全规则稍微复杂一些,您必须手动创建任何查询索引.是否还有其他原因需要使用根集合而不是子集合/集合组查询?

With the advent of collection group queries, it isn't clear to me what benefit there is in using a root collection. In this article by the Firestore team, the only things that I can see is that there is a possibility of name collision, the security rules are slightly more complicated, and you have to manually create any query indices. Are there any other reasons to use a root collection and not subcollections / collection group queries?

推荐答案

在决定使用哪个更好,顶级集合或子集合时,请注意两者都不是更好在速度或计费方面比另一个.但是,有一些细微的差别可以让您选择使用顶级集合而不是子集合.所以唯一的区别可能来自我们构建数据库的方式,而不是性能.这是因为 Firestore 在级别 1 和级别 100 时一样快.

When it comes to deciding, which is better to be used, a top-level collection or a sub-collection, please note that neither of the two is better than the other in terms of speed or billing. However, there are slight differences that can make you choose to use a top-level collection over a sub-collection. So the only differences might come from the way we are structuring the database, rather than performance. This is because Firestore is as fast as it is at level 1 is also at level 100.

假设我们使用子集合具有以下结构:

Let's assume we have the following structure using subcollections:

Firestore-root
  |
  --- shops (collection)
       |
       --- $shopId (document)
              |
              --- products (sub-collection)
                    |
                    --- productId (document)

这个模式也可以设计为:

This schema can also be designed as:

Firestore-root
  |
  --- shops (collection)
  |    |
  |    --- $shopId (document)
  |
  --- products (collection)
       |
       --- $productId (document)
              |
              --- shopId: $shopId

对于 NoSQL 数据库,推荐的做法是将数据库扁平化.为什么?因为它看起来更有条理,而且每个顶级集合只包含与集合本身相关的文档.

When it comes to NoSQL databases, a recommended practice is to have the database flattened. Why? Because it looks more organized and each top-level collection contains only documents that are tied to the collection itself.

说到子系列,当我们看上面的例子时,每个产品都与产品"相关联.子集合、$shopId 和shops";收藏.

When it comes to sub-collections, when we look at the above example, each product is tied to the "products" sub-collection, $shopId, and "shops" collection.

关于安全规则,复杂性取决于您需要使用的过滤器的数量和多样性.但是,对于顶级集合,它会更容易一些.

Regarding security rules, the complexity comes with the number and diversity of filters you need to use. However, for top-level collections, it's a little easier.

最后,请记住,当我们谈论架构时,我们总是根据我们打算执行的查询来构建 Firestore 数据库.因此,您可以在这两个选项之间进行选择,看看哪一个可以让您的生活更轻松.

In the end, bear in mind that when we are talking about architectures, we are always structuring a Firestore database according to the queries that we intend to perform. So it's up to you to choose between these two options and see which one of them can make your life easier.

还有关于结构 Firestore 数据的文档状态:

As also the docs state regarding structure Firestore data:

根级集合适用于多对多关系,并在每个集合内提供强大的查询.

Root-level collections are good for many-to-many relationships and provide powerful querying within each collection.

这篇关于在 Firestore 中使用根集合与子集合相比有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆