所以我需要存储用户创建的帖子,现在data modell是问题所在,将Posts集合中的所有现有帖子都放在一个字段为creatorUserID的地方将使它能够显示属于某个用户的帖子。
现在,一个用户有一个名为Followers的子集,它的ID是关注的人,问题是我不确定如果只显示用户关注的人的帖子,查询会是什么样子。
当集合中有10mio+帖子时,我也会担心性能。
发布于 2019-03-23 20:33:17
为了在Firestore中查询文档,您想要查询的数据必须在您想要查询的文档上,没有办法通过来自另一个集合的文档的数据来查询集合。这就是为什么你的用例有点棘手的原因。它可能看起来不是很优雅,但这是解决它的一种方法:
我们使用两个集合:用户和帖子。
User
- email: string
- followingUserIDs: array with docIds of users you are following
Posts
- postName: string
- postText: string
- creatorUserID: string要查找属于登录用户关注的所有用户的所有帖子,我们可以在代码中执行以下操作:
1检索已登录的用户文档
2对于"followingUserIDs“数组中的每个id,我将查询Firestore以获取帖子。在JavaScript中,它类似于:
followingUserIDs.map(followingUserId => {
return firestore.collection('Posts', ref => ref.where('creatorUserID',
'==', followingUserId));
})3将所有查询的结果组合到一个帖子数组中
https://stackoverflow.com/questions/55312401
复制相似问题