【问题标题】:Firestore query where date (timestamp) is more than 24 hours old日期(时间戳)超过 24 小时的 Firestore 查询
【发布时间】:2020-01-05 08:22:16
【问题描述】:

我的文档中有一个字段,像这样存储为时间戳

  "created_at": admin.firestore.FieldValue.serverTimestamp(),

我想要做的是使用 Cloud Functions 中的管理库查找所有文档,这些文档具有超过 24 小时的此字段

const tsToMillis = admin.firestore.Timestamp.now().toMillis();
const results = await files.where("created_at", ">", tsToMillis - (24 * 60 * 60 * 1000)).get();

我已经尝试了上述方法,但是我不确定如何获取值来比较它,或者有没有办法直接使用 FS 中的时间戳而不转换它?

【问题讨论】:

标签: javascript google-cloud-firestore firebase-admin


【解决方案1】:
const tsToMillis = admin.firestore.Timestamp.now().toMillis();
const compareDate = new Date(tsToMillis - (24 * 60 * 60 * 1000))
const results = await files.where("created_at", "<", compareDate ).get();

【讨论】:

  • 啊,这很有道理,谢谢。刚刚意识到这将是一个&lt; 运算符而不是&gt; :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-07
  • 2019-02-20
  • 2023-03-12
  • 1970-01-01
相关资源
最近更新 更多