Firestore查询-一对多关系

时间:2018-08-27 17:49:52

标签: android firebase firebase-realtime-database nosql google-cloud-firestore

我将开发android移动应用程序,并使用Firebase作为后端。我是NoSql Firestore的新手。

在我的应用中,用户有多个主题。一个主题有很多帖子,帖子有很多评论和一张图片。用户也有很多关注者。

在这种情况下,如何根据自己的需求设计Firestore数据库,最佳实践是什么?如何减少读取次数?

以下结构是否可以满足我的要求?

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

1 个答案:

答案 0 :(得分:2)

简而言之,是的,对于您的特定要求,对我来说看起来不错。今天早些时候,我已经 answered 一个问题,该问题几乎与您一样,并且提供的数据库架构与您已经拥有的相似。但是您需要知道,没有完美的解决方案来构建Cloud Firestore数据库。最好的解决方案是适合您的需求并使您的工作更轻松的解决方案。但是对于这种结构,我发现您可以轻松地查询以获取所有必要的数据,所以我认为继续进行下去。