【问题标题】:Proper Firebase Firestore 'Like' Database Structure in SwiftSwift 中正确的 Firebase Firestore 'Like' 数据库结构
【发布时间】:2017-12-11 18:43:42
【问题描述】:

所以我在实现这一点上进行了相当多的搜索,但似乎找不到如何构建我的数据库的最佳实践。

我正在使用 Firebase 的新数据库 Firestore 创建一个 iOS 应用。

基本上我有两个主要的“收藏”。

'用户' '位置'

有 100 个地点。 用户是无限的。 用户可以“喜欢”位置。 用户可以“关注”其他用户。

我遇到的问题是查询 currentUsers 的“喜欢”位置和“关注”用户。

这些应该是用户内部的对象吗?比如……

/users
    /userId
         name:"Phil"
         likedLocations
             location1:true
             location5:true
         followedUsers
             userId3:true
             userId6:true

或者这些应该在“位置”集合中...

/locations
    /locationId
         name:"New York"
         userLiked
             "userId":true

为了便于编辑,我可能会手动进入数据库并编辑位置。使用选项 A - 这没问题。用户有可能在他们的“likedLocations”中有 100 个项目。使用选项 B - 一个位置可以有无限(与应用中的用户一样多)键值对。

【问题讨论】:

  • TLDR - 建立用户关注/喜欢类型数据库的最容易查询和最具成本效益的方法是什么。
  • 选择第一个。在第二个示例中,纽约只能有 20000 个用户。虽然,菲尔不太可能喜欢 20000 个地点。

标签: database firebase data-structures google-cloud-firestore


【解决方案1】:

为什么不使用这两个根集合?

/likedPlaces
   /userId
      name: "Phil"
      location: locationId

/followers (or followed. Based on your query needs)
   /userId
      name: otherUserId

这样某个用户可以查询:

  • 所有他喜欢的地方

  • 过滤其中一些

  • 他所有的追随者,他们的 id

  • 检查用户是否是他的追随者。

这有意义吗?希望能帮助到你。 亚历山德罗

【讨论】:

    猜你喜欢
    • 2019-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-20
    • 1970-01-01
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    相关资源
    最近更新 更多