Google Cloud Firestore仅查询时间戳早于x的文档

时间:2019-09-04 21:03:09

标签: javascript node.js firebase google-cloud-firestore google-cloud-functions

我正在寻找正确的语法来获取documentSnapshot,其中时间戳字段(FieldValue.serverTimestamp())已存在x天。

例如:在我的收藏集中,每个文档都有一个时间戳字段“ createdAt”。我有一个每日计划的云功能,该功能可以获取和记录“ createdAt"”超过5天的所有文档。

我要使用哪种语法?

1 个答案:

答案 0 :(得分:0)

您可以使用日期对象通过比较过滤查询:

const date = new Date(Date.now() - x * 24 * 60 * 60 * 1000)  // x days ago
collectionRef.where('createdAt', '<', date)