0

I have used database denormalization in Firestore. User can easily sort his friends by his name/letters without an issue. Moreover, User can see “friends of friends” unless friend disabled the visibility setting.

User(collection)
->User ID(Doc)   -> Name: Name0
                 -> visibility: true        
                 -> Friends (sub-collection)
                     ->Friend1 ID (Doc) -> Name: friend1Name
                                        -> visibility: true

                     ->Friend2 ID (Doc) -> Name: friend2Name
                                        -> visibility: true

                     ->Friend3 ID (Doc) -> Name: friend3Name
                                        -> visibility: false

                     ->...

->User ID(Doc)  -> ...  

This structure has borned two issues, when User wanted to sort his friends of friends by the name/letters, he needs to call Friends (sub-collection) of his all friends to make successful sorting. So, the number of call directly based on the number of his friends.

Secondly, when User has changed his visibility setting, it is required to update the visibility of the denormalized data where he is registered inside Friend’s sub-collections. Therefore, if you trigger it from "Cloud Functions", the number of document call directly depending on the number of User’s friends. The cost is open ended for both issues above.

I will be glad if there is an alternative costless structure to cope with those issues in Firestore.

Sunrise17
  • 379
  • 3
  • 18
  • What are the actual queries that you want to perform? – Alex Mamo May 29 '19 at 07:24
  • @AlexMamo, as seen in the link, https://firebase.google.com/docs/firestore/solutions/search , Cloud Firestore doesn't support native indexing or search for text fields in documents. So, do i need to fetch all Friends (sub-collection) data (docs) to the app and then filtering (search friend in my list) inside the app? To cut the long story in short, i want to add search property to the friendList and the friendsOfFriends list. – Sunrise17 May 29 '19 at 08:58
  • Downloading an entire collection to search for friends client-side isn't practical, so please check **[this](https://stackoverflow.com/questions/49596610/is-it-possible-to-use-algolia-query-in-firestorerecycleroptions/49607796)** and **[this](https://stackoverflow.com/questions/52627194/search-by-pattern-on-cloud-firestore-collection/52627798)** out. – Alex Mamo May 29 '19 at 09:09
  • Thanks @AlexMamo, i will check them out. In addition, do you advise to store Friends in map field ( Ex: { friendId: friendName, ... } ) inside document? – Sunrise17 May 29 '19 at 09:13
  • Check also **[this](https://stackoverflow.com/questions/52469492/efficiency-of-searching-using-wherearraycontains)** out. – Alex Mamo May 29 '19 at 09:19
  • Yes sure, instead Algolia, why i have concentrated on map field, because each doc size is about 1MB, and for ex 30 character for userId and average 20 character for username, in total 50 bytes, for 600 friends, its only 30KB? Then in client side pagination can be used to download each friend content. – Sunrise17 May 29 '19 at 09:27
  • Check my answer regarding **[Firestore Pagination](https://stackoverflow.com/questions/50741958/how-to-paginate-firestore-with-android())**. – Alex Mamo May 29 '19 at 09:29

0 Answers0