Firestore查询文档基于2个字段的比较

问题描述 投票:0回答:1

说我有这个文件

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

我想要检索创建后更新的所有userProfiles,这意味着满足createdAt < updatedAt的那些。 你能在Firestore做到吗?

像这样的东西,但第二个操作数是一个字段,而不是一个值:

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

谢谢

firebase google-cloud-firestore
1个回答
1
投票

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

最简单的方法是:

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

要么

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

请注意,您无法查询不包含给定字段的文档(请查看此官方video以获取更多详细信息),因此您不能依赖缺少updatedAt字段。

© www.soinside.com 2019 - 2024. All rights reserved.