【问题标题】:Firestore Social Media Model StructureFirestore 社交媒体模型结构
【发布时间】:2020-01-22 09:51:01
【问题描述】:

我有一个 posts 集合,我的应用程序在其中存储用户的所有帖子,数据结构如下:

+ uid (the user id of the creator)
+ text-content : String
+ tagged-users: [String]
+ assigned-media: [{"downloadUrl": String, "storageLocation": String}]
+ type: String
+ typestamp: Timestamp
+ Likes
    + uid (the user id of the liking person)
    + timestamp: Timestamp
    + postId (optional, depending on the results of the collection group queries)

现在,我想向我的用户显示这个集合的所有帖子;但仅当用户关注创建帖子的人时,即帖子文档的 uid 字段适合。

我知道如果用户只关注一个人,查询集合很简单,即我有一个简单的where 子句。但是:如果我的用户关注 10.000 人 或更多怎么办?怎么查询?

另外,我应该如何存储followings,即用户关注的人?使用简单的数组似乎不适合我,但使用所有其他选项时,我无法在帖子集合中查询来自我关注的人的帖子。

我将不胜感激!

【问题讨论】:

    标签: firebase google-cloud-firestore firebase-cloud-messaging google-cloud-functions firebase-security


    【解决方案1】:

    这是一个复杂的问题(我不能 100% 确定这是提出问题的正确地方),我会尽我所能提供一些东西来帮助你入门,

    免责声明,这可能不适合您的用例

    将您的服务分为 3 个集合: - 用户 - 追随者 - 帖子

    users 收集非常简单,您只需要文档 ID(您已经拥有它)。

    {
      "andyIsTheBest": {
        uid: `andyIsTheBest`
        // ...
      },
      "sophieTheBest": {
         uid: `sophieTheBest`,
         // ...
       },
       "andyTheWorst": {
         uid: `andyTheWorst`,
         // ...
       },
    }
    

    followers 镜像用户的 docs id,应该如下所示:

    {
      "andyIsTheBest": {
         last_post: 2019,
         followers: [
           'sophieTheBest', 'andyTheWorst'
         ],
         recent: [
           {
             post_id: 112233, // from the post collection
             seq: 2019
           }
         ]
      }
    }
    

    最后,posts 集合看起来像这样:

    {
       "112233": {
         seq: 2019,
         body: `blabla the cool post`
         author: `andyIsTheBest`
       }
    }
    

    重要的是,注意序列seqlast_post,你可以通过云函数更新这个值以便你跟踪它,剩下的只是实现细节:

    const remove = firebase.firestore.FieldValue.arrayRemove
    const union = firebase.firestore.FieldValue.arrayUnion
    
    const follow = async (toFollow, user) => {
        await db
        .collection('followers')
        .doc(toFollow)
        .update({ users: union(user) });
    }
    
    const unfollow  = async (toUnFollow, user) => {
        await db
        .collection('followers')
        .doc(toUnFollow);
        .update({ users: remove(user) });
    }
    
    const feed = async (user) => {
       await db.collection(`followers`)
          .where(`followers`, `array-contains`, user)
          .orderBy(`last_post`, `desc`)
          .limit(10)
          .get()
    
       // do whatever business logic you need down here
    }
    

    基本上这样,你有1万亿还是1万亿都没关系,查询应该没问题。

    有很多细节我没有介绍,但这应该可以让你开始。

    希望对您有所帮助。

    感谢在我公司实施此功能的同事^^

    【讨论】:

    • 非常感谢您的回答!我真的很喜欢你的方法,这对我来说也很合乎逻辑-但是最大 1MB 是不是。 Cloud Firestore 中的文档限制?如果用户有 1 个 Mio,这将成为一个问题。追随者?
    • 每个文档最多 1Mb...但您不加载整个文档...例如,您只能拥有文本的缩略图和小预览并保持同步使用您通过云功能存储整个帖子的集合...使用分页来玩这个就可以了...这与查询无关,主要与您如何编排您的应用程序有关
    • 为什么不起作用?您不必同步所有信息,您只需要在此集合中添加您的视图所需的数据......另外,如果每个文档的限制对您来说太多了,firestore 可能不是您问题的正确解决方案,我很难想象你需要为大于 1Mb 的社交媒体存储哪些信息
    • 感谢您的回复。看一下followers 集合:在那里,你有一个Array 类型的字段“follower”。如果一个用户有 100 万个关注者,那么数组将存储 100 万个用户 ID,对吗?但是,1MB 的文档大小限制不允许数组变得那么大。剩下的就可以了。
    • 是的,但是这部分呢:followers: [uid1, uid2]?在那里我会存储跟随我的人的所有用户ID,对吗?如果有 100.000 人关注我怎么办?我不太明白
    猜你喜欢
    • 2023-03-26
    • 2021-02-12
    • 2019-04-20
    • 2019-06-10
    • 1970-01-01
    • 2018-08-28
    • 2014-10-12
    • 2019-10-06
    • 2019-10-26
    相关资源
    最近更新 更多