Firebase Firestore社交媒体模型结构

Firebase Firestore社交媒体模型结构,firebase,google-cloud-firestore,firebase-cloud-messaging,google-cloud-functions,firebase-security,Firebase,Google Cloud Firestore,Firebase Cloud Messaging,Google Cloud Functions,Firebase Security,我有一个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

我有一个
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
子句。但是:如果我的用户跟踪10000人或更多人怎么办?我怎么能对此提出质疑

此外,我应该如何存储
以下内容
,即用户关注的人?使用一个简单的数组似乎不适合我,但是使用所有其他选项,我无法查询posts集合中来自我关注的人的帖子


我将非常感激能得到的一切帮助

这是一个复杂的问题(我不能100%确定这是一个合适的地方),我会尽我最大的努力让你开始

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

将您的服务分为3个集合: -使用者 -追随者 -职位

用户收集非常简单,您只需要文档id(您已经有了它)

followers镜像用户的文档id,它应该如下所示:

{
  "andyIsTheBest": {
     last_post: 2019,
     followers: [
       'sophieTheBest', 'andyTheWorst'
     ],
     recent: [
       {
         post_id: 112233, // from the post collection
         seq: 2019
       }
     ]
  }
}
{
   "112233": {
     seq: 2019,
     body: `blabla the cool post`
     author: `andyIsTheBest`
   }
}
最后,posts集合如下所示:

{
  "andyIsTheBest": {
     last_post: 2019,
     followers: [
       'sophieTheBest', 'andyTheWorst'
     ],
     recent: [
       {
         post_id: 112233, // from the post collection
         seq: 2019
       }
     ]
  }
}
{
   "112233": {
     seq: 2019,
     body: `blabla the cool post`
     author: `andyIsTheBest`
   }
}
重要信息,请注意序列
seq
编号以及
last\u 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万亿,查询应该是可以的

有很多细节我没有涵盖,但这应该可以让你开始

希望这有帮助


感谢我的同事们在我的公司实施了此计划^ ^

非常感谢您的回答!我真的很喜欢你的方法,在我看来这也是合乎逻辑的——但CloudFireStore中不存在最大1MB的文档限制吗?如果一个用户有一百万,这将成为一个问题。追随者?每个文档最多有1Mb。。。但是你不需要加载整个文档。。。例如,您可以只拥有文本的缩略图和小预览,并将其与通过云函数存储整个帖子的集合保持同步。。。将此与此一起使用分页将完成此操作。。。这与查询无关,主要与您如何编排应用程序有关。如果一个用户跟踪2K人,那么这将不起作用,因为所有跟踪的孩子都存储在一个数组中。Firebase Firestore文档的大小限制为1MB,这将被超过。您是否知道如何对数据建模以避免1MB限制?为什么它不起作用?您不必同步所有信息,只需在此集合中添加查看所需的数据。。。此外,如果每个文档的限制对您来说太多,那么firestore可能不是解决您问题的正确解决方案,我很难想象对于大于1Mb的社交媒体,您需要存储哪些信息