【问题标题】:Query to show all firestore documents not marked as viewed by a user查询以显示所有未标记为用户已查看的 Firestore 文档
【发布时间】:2022-01-15 10:23:40
【问题描述】:

我正在将 Cloud Firestore 与 Flutter 结合使用,并向礼品购买者展示产品以供礼品接收者使用。

我想向 giftBuyers 推荐 giftRecipients 的大量产品。因为 products 的列表很大,我想避免将它复制到每个 giftBuyer 的每个 giftRecipient

我希望 products 只向给定 giftRecipientgiftBuyer 显示一次。但是,我希望将来能够搜索 giftBuyergiftRecipient 查看的所有 产品

我开始使用的架构是:

  • 产品
    • 产品1
      • 商家名称
      • 产品名称
      • giftBuyerCollection
      • 礼物买家 A
        • giftRecipients
        • 礼物收件人 X
          • productViewed: true
        • giftRecipient Y
    • 礼物买家 B
      • giftRecipients
      • giftRecipient X
      • giftRecipient Y
  • 产品 2
  • 当我获取产品时,我想创建一个查询

    pseudocode{
    return all product documents where
    .collection(giftBuyerCollection)
    .doc(giftBuyer A)
    .collection(giftRecipients)
    .doc(giftRecipient X)
    .field(productViewed does not exist)
    }
    

    这可能吗?如果是这样,它会涉及多个查询吗?这似乎是 collectionGroup 的用例,但我不确定如何使用 ID 来构建它。

    我考虑的另一个替代模式是

    • 产品
      • 产品1
        • 商家名称
        • 产品名称
        • giftBuyer A-giftRecipient X:真
        • giftBuyer A-giftRecipient Y: true
      • 产品2
        • 商家名称
        • 产品名称
        • giftBuyer B-giftRecipient X:真
        • giftBuyer B-giftRecipient Y: true

    使用where() 方法会更好,但对于安全规则来说似乎是不可能的。

    欢迎提供其他架构建议。

    这些提要和聊天建议似乎不适合我的用例。

    Firebase Firestore Structure for getting un-seen trending posts - Social

    Firebase for getting unread posts

    How to get all documents in a firestore subcollection based on the parent´s doc values?

    Firestore: Get subcollection of document found with where

    【问题讨论】:

      标签: firebase flutter google-cloud-firestore


      【解决方案1】:

      我认为我有一个潜在的解决方案,但它需要云功能和非规范化数据。看看这个example of a follow/unfollow system,产品数据库可以构造为

      • 产品

        • 产品 1

          • 商家名称

          • 产品名称

          • giftBuyer A-giftRecipient X: true

          • giftBuyer A-giftRecipient Y: true

        • 产品 2

          • 商家名称

          • 产品名称

          • giftBuyer B-giftRecipient X: true

          • giftBuyer B-giftRecipient Y: true

      这里的权限设置为所有授权用户都可以读,但没有人可以写。

      然后当礼物购买者在搜索礼物收件人时,他们会写入自己查看的产品列表

      • giftBuyers
        • 礼物买家 A
          • 礼物收件人
            • 礼物收件人 X
              • 查看产品
                • 产品1
                • 产品 2
            • 礼物收件人 Y
              • 查看产品
                • 产品1
                • 产品 2

      然后我可以使用触发的云功能从giftBuyer查看的产品写入产品页面。

      然后是工作流程

      1. 查询数据库并询问所有没有字段“{$giftBuyer}-{$giftRecipient}”的产品。
      2. 将所有查看过的产品写入该 giftBuyer。
      3. 每次我们分页,触发云函数从giftBuyer集合写入products集合。

      【讨论】:

        猜你喜欢
        • 2016-05-14
        • 2022-01-20
        • 2018-05-09
        • 2020-06-18
        • 1970-01-01
        • 2017-08-29
        • 2021-11-04
        • 2020-01-05
        • 2019-11-01
        相关资源
        最近更新 更多