【问题标题】:Flutter & firestore: query all posts from multiple usersFlutter & firestore:查询多个用户的所有帖子
【发布时间】:2021-06-12 00:32:11
【问题描述】:

我的数据库结构如下:

每个用户在以下集合中都有一个文档,该文档还包含一个集合,其中包含他所关注的用户的 ID。

“posts”集合具有发布它的用户的 ID。

现在我想以这样一种方式查询它,以便从我的用户关注的用户那里获取所有帖子(按时间戳排序)

这是我的用户和帖子模型的样子:

class PostModel {
  final String id;
  final String creator;
  final String text;
  final Timestamp timestamp;
  
  
  PostModel({this.id, this.creator, this.text, this.timestamp});
}
class UserModel {
  final String id;
  final String email;
  final int following;
  final int followers;
  final String profileImgUrl;
  final String name;
  final String bio;
  final bool isVerified;
  final int postAmount;

  UserModel(
      {this.id,
      this.email,
      this.following,
      this.followers,
      this.profileImgUrl,
      this.name,
      this.bio,
      this.isVerified,
      this.postAmount});
}

有人可以帮我想出一个简单的方法来做这件事吗?

【问题讨论】:

    标签: flutter dart google-cloud-firestore


    【解决方案1】:

    您可以有一个数组字段来存储用户,而不是子集合用户,否则您需要每次都获取用户子查询,这很昂贵

    【讨论】:

    • 但这会限制某人可以拥有的关注者数量,对吗?
    • 文档大小限制为 1 mb 如果您认为这还不够,您可以创建一个新的 PostFollowing 集合,其中每个文档的 id 为 userId+postID 然后您可以轻松查询用户的所有帖子并对其进行分页
    猜你喜欢
    • 2023-03-09
    • 2018-08-16
    • 2020-06-04
    • 2020-04-23
    • 2019-05-14
    • 2023-04-05
    • 2019-08-01
    • 1970-01-01
    • 2020-12-07
    相关资源
    最近更新 更多