Firestore Cloud Function 按日期查询

Al *_* Xx 5 javascript firebase google-cloud-platform google-cloud-firestore

我试图查询 Firestore 数据库中的集合并按日期过滤。我有一个时间戳字段,当名为“specicDate”的字段等于 now() 时,我需要获取结果。

app.get('/execute',  async(req, res, next) => {

  const snaps = await db.collection('notifications').where("specificDate", "==", new Date()).get()

  const results= [];
  snaps.forEach(snap => results.push(snap.data()));
  res.status(200).json({results});
});
Run Code Online (Sandbox Code Playgroud)

无论任何组合,我都没有结果。如果我用“<”或“">”更改“==”,我就会得到结果。恐怕问题与时间有关,我想忽略这一点,我只关心年、月和日。下图展示了我的 firestore 收藏,其中只有一件 1 件。 在此输入图像描述

我一直在研究,但我还不够幸运。

Jan*_*Jan 8

使用 AND 查询为查询提供所需的范围。由于您不关心精确匹配时间,因此请为其指定整个天数范围。

var start = new Date();
start.setHours(0,0,0,0);

var end = new Date(start.getTime());
end.setHours(23,59,59,999);

const snaps = await db.collection('notifications').where("specificDate", ">=", start ).where("specificDate", "<=", end).get()
Run Code Online (Sandbox Code Playgroud)