2

I am developing an app that will offer the possibility to filter the users you search (gender, age, location, ...). I want to make this fast and as cheaper as possible, so I have decided to store my data as a n-ary tree, just something like this:

----- Users -----

Root Node: Gender (male or female)

-male
   --country (nodes: USA, UK, Spain, Germany, France, Italy, ...)
      ---age (nodes: 18, 19, 20, 21, 22, ..., 60)
         ->user (leaf nodes)

-female
   --country (nodes: USA, UK, Spain, Germany, France, Italy, ...)
     ---age (nodes: 18, 19, 20, 21, 22, ..., 60)
        ->user (leaf nodes)

I think that structuring the database like this will reduce the amount of data, as I don't have to store the gender, country, and age as field in the last documents... But I don't know if this is a bad practices in the NoSql world, or if this will negativily affect the performance (velocity).

Any ideas about if this is a good structure? Or should I store all this properties in every user document and use the "where" query clausure? Thank you.

Raul
  • 2,673
  • 1
  • 15
  • 52
  • What do you mean about n-art tree? Are the nodes collections, documents, collection, ... until the leafs that are documents? Maybe you are looking for data denormalization. https://stackoverflow.com/questions/62993392/firestore-with-complex-filter-and-composite-indexes/63029341?noredirect=1#comment112158130_63029341 Check @AlexMamo answer – Victor Molina Aug 15 '20 at 16:25
  • With this you can create a collection called “country” for example, with a lot of documents that contain the users ids. Just follow the tree road, from the root to the leave in which your solution is and do different queries to those collections. – Victor Molina Aug 15 '20 at 16:28
  • Yeah, I know what you mean but I am talking about having multiple subcollections. I don't know if is better to go on with data denormalization because I will have to do more queries. – Raul Aug 15 '20 at 16:54

1 Answers1

1

In your scenario you want to filter on 3 attributes (gender, country and age) and I think it could work if all three attributes are provided.

If one of the three is missing you could use denormalization to replicate data on multiple collections, but since you want it cheap I presume you don't want to increase on the storage usage.

Another option would be to store them all inside a single user collection and query with where conditions taking advantage of indexing. The indexes are created on you behalf and they are the reason Firestore is so fast.

Ajordat
  • 1,320
  • 2
  • 7
  • 19