Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most efficient way to store tags in Firestore?

The NoSQL Firestore has no table, what will be the best way for tagging, just store multiple tags in an array?

like image 775
MubieSam Lin Avatar asked Dec 24 '22 01:12

MubieSam Lin


1 Answers

The NoSQL Firestore has no table.

That's right, the database is in a JSON format.

What will be the best way for tagging, just store multiple tags in array?

According to the use case of your app, you can choose between two approaches. If your tags are of type String, then you can store this literal strings in an array. This would be the first approach and the database schema might look like this:

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

As you can see, I took as an example a collection of questions in which each document has an array of tags.

If you need more details about a tag, the second approach would be to create an object for each tag and store this tag objects in a collection. In the question document, you'll only need to store the ids of the tags also in an array, as in the following schema:

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
like image 127
Alex Mamo Avatar answered Jun 14 '23 17:06

Alex Mamo