Firebase Firestore数据建模和查询类似Tinder的应用程序

Firebase Firestore数据建模和查询类似Tinder的应用程序,firebase,nosql,google-cloud-firestore,data-modeling,Firebase,Nosql,Google Cloud Firestore,Data Modeling,为了让事情更简单,假设应用程序只有两个实体:用户创建的user和post 用户可以做3件事: 创建帖子 刷帖子,喜欢还是不喜欢 取消跟踪某个帖子的作者,以便他/她不再看到该作者的帖子 我的问题是:如何根据作者是否未被我跟踪来查询帖子 我的第一种数据建模方法 但是我不知道如何使用unfollows查询posts,这是一个有效的用户ID数组 // This doesn't seem correct and // I don't know how to write the `where` filter

为了让事情更简单,假设应用程序只有两个实体:用户创建的
user
post

用户可以做3件事:

  • 创建帖子
  • 刷帖子,喜欢还是不喜欢
  • 取消跟踪某个帖子的作者,以便他/她不再看到该作者的帖子
  • 我的问题是:如何根据作者是否未被我跟踪来查询
    帖子

    我的第一种数据建模方法 但是我不知道如何使用
    unfollows
    查询
    posts
    ,这是一个有效的用户ID数组

    // This doesn't seem correct and
    // I don't know how to write the `where` filters programmatically:
    
    // assume unfollows is already sorted.
    const unfollows = collection('posts')
      .where('author', '<', unfollows[0]).where('author', '>', unfollows[0])
      .where('author', '<', unfollows[1]).where('author', '>', unfollows[1])
      ...
    
    这使我能够查询
    帖子

    collection('posts').where(`unfollowedUserId.${myUserId}`, '==', null);
    
    然而,这种方法存在一些问题:

  • post
    文档中的
    unfollowdby
    不可缩放,因为
    unfollowdby
    的大小与用户群的大小成比例。我知道,对于任何将增长到无限大小的内容,都应该是子集合,而不是文档中的映射,但是通过将
    取消子集合设置为
    子集合,我无法再使用它查询
    帖子
  • 如果作者经常被取消跟踪,则
    post
    文档将经常更新,以使
    地图保持最新
  • 任何用户都需要权限才能更新他人帖子的
    unfollowdby
    字段,这并不理想,否则我将不得不创建一个云函数来处理unfollowd

  • 你在这方面有什么进展吗?我也有同样的问题。我得出的结论是firestore根本不适合这种应用=/
    // This doesn't seem correct and
    // I don't know how to write the `where` filters programmatically:
    
    // assume unfollows is already sorted.
    const unfollows = collection('posts')
      .where('author', '<', unfollows[0]).where('author', '>', unfollows[0])
      .where('author', '<', unfollows[1]).where('author', '>', unfollows[1])
      ...
    
    users/{userId}
    - userId
    
    posts/{postId}
    - author <userId>
    - content
    - unfollowedBy <Map with userIds as keys>
    
    unfollows/{userId}
    - userId
    - unfollowedUserId
    
    likes/{postId_userId}
    - postId
    - userId
    
    dislikes/{postId_userId}
    - postId
    - userId
    
    collection('posts').where(`unfollowedUserId.${myUserId}`, '==', null);