【问题标题】:Can I query for documents and filter some document fields out of the querySnapshot response using Firestore?我可以使用 Firestore 查询文档并从 querySnapshot 响应中过滤一些文档字段吗?
【发布时间】:2019-10-17 06:23:07
【问题描述】:

假设我有一组文档:

收藏: 'products'

在该集合中我有文档,每个文档都有一个非常重的字段,我希望在将其发送给我的客户之前对其进行过滤。

文档 product1:

{
  id: 'someUniqueKey',
  title: 'This is the title',
  price: 35.00,
  heavyField: [
    'longStringxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    'longStringxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    'longStringxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    'longStringxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    'longStringxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    'longStringxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
}

我可以查询 products 集合中的那些文档,并将这个特定的 heavyField 排除在我的回复之外吗?

我希望能够过滤掉其中 1 个字段的文档。也许选择我想在我的客户端上接收的字段。

这可能吗?或者在这种情况下,最好以不同的方式构建我的数据并将heavyField 留在不同的集合中?

【问题讨论】:

    标签: firebase google-cloud-firestore


    【解决方案1】:

    在 SQL 中,这将在查询中称为“projection”。 Cloud Firestore 不支持来自网络和移动客户端 SDK 的投影。相反,您应该将“重”字段放入另一个集合中的文档中,并仅在需要时进行查询。

    如果您想知道为什么,部分原因与客户端本地持久层的工作方式有关。如果给定文档仅在本地存在某些字段,则缓存会变得更加复杂。简单地获取和存储整个文档要简单得多,因此文档是否在本地可用是毫无疑问的。

    出于数据建模的目的,最好将文档视为“原子单元”,要么全部要么全部,不能分解。

    【讨论】:

      猜你喜欢
      • 2021-05-04
      • 2019-03-21
      • 2021-09-05
      • 1970-01-01
      • 2018-06-04
      • 2021-05-11
      • 2019-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多