在Firestore中存储标签的最有效方法是什么?

时间:2019-01-16 21:42:29

标签: firebase google-cloud-firestore

NoSQL Firestore没有表,什么是最好的标记方式,只需将多个标记存储在一个数组中?

1 个答案:

答案 0 :(得分:3)

  

NoSQL Firestore没有表。

是的,数据库是JSON格式。

  

将标签存储在数组中的最佳方式是什么?

根据您的应用程序的用例,您可以在两种方法之间进行选择。如果标签的类型为String,则可以将此文字字符串存储在数组中。这将是第一种方法,数据库架构可能如下所示:

Firestore-root
    |
    --- questions (collections)
          |
          --- questionId (document)
                 |
                 --- questionId: "02cubnmO1wqyz6yKg571"
                 |
                 --- title: "Question Title"
                 |
                 --- tags ["History", "Geography"]

如您所见,我以一个问题集合为例,其中每个文档都有一个标签数组。

如果您需要有关标签的更多详细信息,第二种方法是为每个标签创建一个对象并将该标签对象存储在集合中。在问题文档中,您只需要将标记的ID也存储在数组中即可,如以下模式所示:

Firestore-root
    |
    --- questions (collections)
    |     |
    |     --- questionId (document)
    |            |
    |            --- questionId: "02cubnmO1wqyz6yKg571"
    |            |
    |            --- title: "Question Title"
    |            |
    |            --- tags ["tagId", "tagId"]
    |
    --- tags (collections)
          |
          --- tagId (document)
          |     |
          |     --- tagId: "yR8iLzdBdylFkSzg1k4K"
          |     |
          |     --- tagName: "History"
          |     |
          |     --- //Other tag properties
          |
          --- tagId (document)
                |
                --- tagId: "tUjKPoq2dylFkSzg9cFg"
                |
                --- tagName: "Geography"
                |
                --- //Other tag properties