【问题标题】:Question: What is the best DB structure when adding follow/followers feature?问题:添加关注/关注者功能时最好的数据库结构是什么?
【发布时间】:2019-07-22 19:42:03
【问题描述】:

我目前有一个具有以下数据库结构的应用程序,它使用 Firebase 数据库并且在 Swift IOS 上:

  "Posts" : {
"Dm8iyaXXdTOJGsymEiLNVO6OdDK2" : {
  "post:570915537" : {
    "Media" : {
      "image" : {
        "mediaUrl" : "https://firebaseURL",
        "postTimeStamp" : 5.70915539085856E8,
        "timeStamp" : 5.7091551482329E8
      }, ...

我现在要为其添加关注者。我在想我会添加一个全新的组:

  "Followers" : {
"Dm8iyaXXdTOJGsymEiLNVO6OdDK2" : {
  "Following" : {
    follower1: "Dm8iyaXXdTOJGsymEiLNVO6OdDK2";
    //other followers
  }, ...

或者向原始组添加一个新节点并将它们添加到那里。最后一个选项是在“用户”部分执行与上面所做的类似的操作。

最好的做法是什么?

【问题讨论】:

  • 对于 NoSQL 数据库,“最佳”结构是满足构建应用程序所需的特定查询的结构。
  • @dougstevenson 我们正在使用 firebase
  • Firebase 有两个数据库系统,Realtime Database 和 Cloud Firestore。两者都是 NoSQL 类型的数据库。
  • 我在这里删除了 ios 和 swift 标签,因为您的问题与其中任何一个都无关。

标签: database firebase firebase-realtime-database database-design


【解决方案1】:

使用 Firestore 的关注/取消关注架构可能是这个: 2 个根集合,一个包含 用户,另一个通过复合键保存用户之间的跟随关系

users/{userID}
  .. userData
  .. followerCount
  .. followedCount


following/{followerID_followedID}
  .. followerId
  .. followedId
  .. createdAt

当用户A开始关注用户B时:

  • 以下收藏。
  • 触发将运行事务以更新 两个用户的计数器。

当用户停止关注另一个用户时,做相反的事情。

在客户端,您可以通过检查文档 userAuid_userBuid 是否存在于 following 集合中来知道 userA 是否跟随 userB。 也可以通过查询followedId ==当前用户Id的集合来获取用户的关注者列表。

希望对你有所帮助。

【讨论】:

  • 以下节点中的createdAt 是什么!?
猜你喜欢
  • 2013-11-13
  • 2012-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-10
  • 2010-10-16
  • 1970-01-01
相关资源
最近更新 更多