Firebase localised tags data structure that allow filtering by tags

Viewed 51

I want to list all data from /products collection. But I also want to get data from /products collection filtered by tags.

So the case is: get all products that have tags red, greed and blue. Tags are also searchable in app by specific language but ID should be the same that is why I am referencing IDs from products.

My design idea:

/tags
  -tag_id_1: {en: "red", de: "rot"}
  -tag_id_2: {en: "green", de: "grün"}
  -tag_id_3: {en: "blue", de: "blau"}

/products
  -PRODUCT_1
    -title: ""
    -description: ""
    -date: ""
    -tags: [tag_id_1,tag_id_2,tag_id_3] 
  -PRODUCT_2
    -title: ""
    -description: ""
    -date: ""
    -tags: [tag_id_3] 

Now to the problem:

  • Should I duplicate selected tags translations in each product and have tagIds:[] and tags: {{tag},{tag}...}
  • With this structure is it possible to fetch all products that has tag_id_1,tag_id_2,tag_id_3 ordered by date?
1 Answers

Since the tags in your schema contain colors in different languages, a possible database schema that can help you query all products by specific colors might look like this:

Firestore-root
   |
   --- products (collection)
         |
         --- $productId (document)
                |
                --- title: "T-shirt"
                |
                --- description: "It's awesome."
                |
                --- date: "2022-07-22"
                |
                --- colors (map)
                     |
                     --- en
                     |   |
                     |   --- red: true
                     |   |
                     |   --- green: true
                     |   |
                     |   --- blue: true
                     |
                     --- de
                         |
                         --- rot: true
                         |
                         --- grün: true
                         |
                         --- blau: true

A user can only have a single language set on the device. That being said, if the device is set for example, in English, to get all products that can be red, green as well as blue, the following query is required:

db.collection("products")
        .whereEqualTo("colors.en.red", true)
        .whereEqualTo("colors.en.green", true)
        .whereEqualTo("colors.en.blue", true);

Remember that this kind of query is an AND and not an OR.

Related