简体   繁体   中英

Firestore Query - One to Many Relationship

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM