7

I am going to develop android mobile app and going to use Firebase as backend. I am new to NoSql Firestore.

In my app an user has many Topics. A Topic has many posts and the post has many comments & one image. Also user has many followers.

In this scenario, how can I design Firestore database for my requirement and what is the best practice? How to reduce no.of read ?

Is following structure OK for my requirement?

Firestore-root
    |
    --- Topic (collections)
    |     |
    |     --- topicId (document)
    |            |
    |            --- topicId: "345"
    |            |
    |            --- title: "Topic Title"              
    |            |
    |            --- viewCount: 20
    |            |
    |            --- likeCount: 30
    |            |
    |            --- Posts (colletion)
    |            |     |
    |            |     --- postId
    |            |            |
    |            |            ---postId: "321"
    |            |            |
    |            |            ---Title: "My title"
    |            |            |
    |            |            --- Image: ' '
    |            |            |
    |            |            --- commentsCount: 1
    |            |            |
    |        |            --- comments (colletion)
    |            |          |
    |            |           --- commentId
    |            |                             |
    |            |                            ---commentId: "123"
    |            |                        |
    |            |                   ---comment: "My Comment"
    |            |                             |    
    |            --- likes (colletion)
    |            |     |
    |            |     --- likeId (document)
    |            |            |
    |            |            --- userId: true
    |            | 
    |         
    |
     --- Users (collections)
    |     |
    |     --- userId (document)
    |            |
    |            --- userId: "345"
    |            |
    |            --- name: "Test Name"              
    |            |
    |            --- followersCount: 20
    |            |
    |            --- followingCount: 30
static const
  • 953
  • 4
  • 16
AD Mark
  • 115
  • 1
  • 7
  • 1
    Please could you explain the "userId: true"?? Shouldn´t us save the actual userId value like "CBXSHUYVXUVXUWC"?? so save userID: "CBXSHUYVXUVXUWC" – Juan Jan 01 '21 at 19:42

1 Answers1

7

As a short an answer, yes, looks good to me for your particular requirements. I already answered a question earlier today, which is almost as yours and which I have provided a similar database schema as you already have. But you need to know that there is no perfect solution for structuring a Cloud Firestore database. The best solution, is the solution that fits your needs and makes your job easier. But regarding this structure, I see that you can easely query to get all necessary data, so in my opinion, go ahead with that.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thanks Alex. I want to show no.of comments in the Topic and the post. Is it good in comments collection under Topic collection instead of post collection? – AD Mark Aug 28 '18 at 02:54
  • It is good to have the `comments` collection under `posts` collection, more precisely under the `postId` because all the comments correspond to a particular post. If you want to have comments also for every topic, then go ahead in add another collection named `comments` but this time directly under `topicId` document right? – Alex Mamo Aug 28 '18 at 07:54