【问题标题】:Firestore Query - One to Many RelationshipFirestore 查询 - 一对多关系
【发布时间】:2019-02-02 07:14:54
【问题描述】:

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

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

在这种情况下,我如何根据我的要求设计 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

【问题讨论】:

  • 请您解释一下“userId: true”吗?我们不应该像“CBXSHUYVXUVXUWC”那样保存实际的userId值吗?所以保存用户ID:“CBXSHUYVXUVXUWC”

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


【解决方案1】:

作为一个简短的答案,是的,对我来说很适合您的特定要求。我今天早些时候已经 answered 提出了一个问题,这几乎与您的问题一样,并且我提供了与您已经拥有的类似的数据库架构。但您需要知道,构建 Cloud Firestore 数据库没有完美的解决方案。最好的解决方案是满足您的需求并使您的工作更轻松的解决方案。但是关于这种结构,我看到您可以轻松查询以获取所有必要的数据,所以我认为,请继续。

【讨论】:

  • 谢谢亚历克斯。我想在主题和帖子中显示没有 cmets。主题收藏而不是帖子收藏下的cmets收藏好不好?
  • 最好将comments 集合放在posts 集合下,更准确地说是在postId 下,因为所有的cmets 都对应一个特定的帖子。如果您还想为每个主题添加 cmets,请继续添加另一个名为 comments 的集合,但这次直接在 topicId 文档下,对吗?
猜你喜欢
  • 2018-07-08
  • 2014-07-14
  • 2015-03-31
  • 2014-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-23
  • 1970-01-01
相关资源
最近更新 更多