【问题标题】:Firestore query document based on comparison of 2 fields基于 2 个字段比较的 Firestore 查询文档
【发布时间】:2019-08-14 08:40:55
【问题描述】:

说我有这个文件

userProfile: {
  createdAt: '2018-01-01',
  updatedAt: '2018-01-04'
}

我想检索所有在创建后更新的userProfiles,即满足createdAt < updatedAt 的那些。
你能在 Firestore 中做到这一点吗?

类似这样,但第二个操作数是字段,而不是值:

userProfileRef.where("createdAt", "<", "updatedAt")

谢谢

【问题讨论】:

    标签: firebase google-cloud-firestore


    【解决方案1】:

    您将无法使用“标准”Firestore 查询来执行此操作。

    最简单的方法是:

    1. 如果文档在创建后从未更新,则在 updatedAt 字段中具有默认的“虚拟”日期(例如 1900-01-01)。然后您将使用userProfileRef.where("updatedAt", "&gt;", "1900-01-01") 进行查询

    1. 在文档中有一个特定的标志,当您更改 updatedAt 字段的值时(即在创建后更新文档时),您将更新该标志。

    请注意,您无法查询不包含给定字段的文档(有关详细信息,请观看此官方 video),因此您不能依赖 updatedAt 字段的缺失。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-04
      • 2018-12-20
      • 2021-09-01
      • 1970-01-01
      • 2021-05-11
      • 2015-01-31
      • 2018-06-04
      相关资源
      最近更新 更多