在Firestore中使用子集合有什么好处? [英] Are there any benefits of using subcollections in firestore?

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

问题描述

我的应用程序用户集合中的每个文档都有一个子集合.该子集合存储与用户相关的文档,但是也可以将它们保存到主集合中,每个文档都具有关联的userId.

I have a subcollection for each doc in the users collection of my app. This subcollection stores docs that are related to the user, however they could just as well be saved to a master collection, each doc with an associated userId.

我选择了这种结构,因为它在当时似乎最为明显,但是我可以想象,如果需要进行数据库维护,它将使事情变得更加艰难.例如.如果要清理这些文档,则必须先查询每个用户,然后再查询每个用户的文档,而如果我有一个主集合,则可以查询所有文档.

I chose this structure as it seemed the most obvious at the time but I can imagine it will make things harder down the road if I need to do database maintenance. E.g. If I wanted to clean up those docs, I would have to query each user and then each users docs, whereas if I had a master collection I could just query all docs.

这使我质疑子集合的意义是什么,如果您可以将这些文档与ID关联起来的话.仅仅是在那里,以便您的文档接近1MB限制时可以扩展吗?

That lead me to question what is the point of subcollections at all, if you can just associate those docs with an ID. Is it solely there so that you can expand if your doc becomes close to the 1MB limit?

推荐答案

让我们举个例子.假设我们有一个测验应用程序的数据库架构,如下所示:

Let's take an example for that. Let's assume we have a database schema for a quiz app that looks like this:

Firestore-root
    |
    --- questions (collections)
          |
          --- questionId (document)
                 |
                 --- questionId: "LongQuestionIdOne"
                 |
                 --- title: "Question Title"
                 |
                 --- tags (collections)
                      |
                      --- tagIdOne (document)
                      |     |
                      |     --- tagId: "yR8iLzdBdylFkSzg1k4K"
                      |     |
                      |     --- tagName: "History"
                      |     |
                      |     --- //Other tag properties
                      |
                      --- tagIdTwo (document)
                            |
                            --- tagId: "tUjKPoq2dylFkSzg9cFg"
                            |
                            --- tagName: "Geography"
                            |
                            --- //Other tag properties

其中tagsquestionId对象内的子集合.现在,将tags集合创建为顶级集合,如下所示:

In which tags is a subcollection within questionId object. Let's create now the tags collection as a top-level collection like this:

Firestore-root
    |
    --- questions (collections)
    |     |
    |     --- questionId (document)
    |            |
    |            --- questionId: "LongQuestionIdOne"
    |            |
    |            --- title: "Question Title"
    |
    --- tags (collections)
          |
          --- tagIdOne (document)
          |     |
          |     --- tagId: "yR8iLzdBdylFkSzg1k4K"
          |     |
          |     --- tagName: "History"
          |     |
          |     --- questionId: "LongQuestionIdOne"
          |     |
          |     --- //Other tag properties
          |
          --- tagIdTwo (document)
                |
                --- tagId: "tUjKPoq2dylFkSzg9cFg"
                |
                --- tagName: "Geography"
                |
                --- questionId: "LongQuestionIdTwo"
                |
                --- //Other tag properties

这两种方法之间的区别是:

The differences between this two approaches are:

  • 如果要查询数据库以获取特定问题的所有tags,则使用第一个模式非常简单,因为只需要一个CollectionReference(问题-> questionId->标记).要使用第二个模式来实现同一目的,而不是CollectionReference,需要一个Query,这意味着您需要查询整个tags集合以仅获取与单个问题相对应的标记. /li>
  • 使用第一个架构,一切都将井井有条.除此之外,在Firestore中,子集合的最大深度:100 .这样您就可以利用它了.
  • @RenaudTarnec在其评论中也提到,Cloud Firestore中的查询很浅,它们仅从运行查询的集合中获取文档.无法通过单个查询从顶级集合以及其他集合或子集合中获取文档. Firestore不一次性支持跨不同集合的查询.单个查询只能使用单个集合中的文档属性.因此,您无法使用第一个架构来获取所有问题的所有标签.
  • If you want to query the database to get all tags of a particular question, using the first schema it's very easy because only a CollectionReference is needed (questions -> questionId -> tags). To achieve the same thing using the second schema, instead of a CollectionReference, a Query is needed, which means that you need to query the entire tags collection to get only the tags that correspond to a single question.
  • Using the first schema everything is more organised. Beside that, in Firestore Maximum depth of subcollections: 100. So you can take advantage of that.
  • As also @RenaudTarnec mentioned in his comment, queries in Cloud Firestore are shallow, they only get documents from the collection that the query is run against. There is no way to get documents from a top-level collection and other collections or subcollections in a single query. Firestore doesn't support queries across different collections in one go. A single query may only use properties of documents in a single collection. So there is no way you can get all the tags of all the questions using the first schema.

这项技术称为数据库展平,在Firebase中是一种非常普遍的做法.因此,仅在需要时才使用此技术.因此,在您的情况下,如果只需要显示单个问题的标签,请使用第一个模式.如果您想以某种方式显示所有问题的所有标签,则建议使用第二种模式.

This technique is called database flatten and is a quite common practice when it comes to Firebase. So use this technique only if is needed. So in your case, if you only need to display the tags of a single question, use the first schema. If you want somehow to display all the tags of all questions, the second schema is recommended.

它是否仅存在于其中,以便您的文档接近1MB的限制时可以扩展?

Is it solely there so that you can expand if your doc becomes close to the 1MB limit?

如果文档中有对象的子集合,请注意,该子集合的大小不计入该1 MiB限制.仅计算存储在文档属性中的数据.

If you have a subcollection of objects within a document, please note that size of the subcollection it does not count in that 1 MiB limit. Only the data that is stored in the properties of the document is counted.

编辑2019年10月1日:

根据@ShahoodulHassan评论:

According to @ShahoodulHassan comment:

因此,您无法使用第一个模式获取所有问题的所有标签吗?

So there is no way you can get all the tags of all the questions using the first schema?

实际上已经有了,我们可以使用

Actually now there is, we can get all tags of all questions with the use of Firestore collection group query. One thing to note is that all the subcolletions must have the same name, for instance tags.

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

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