如何根据时间戳过滤 documentdb 记录?
How can I filter documentdb records based on timestamp?
我一直在使用 documentdb 数据库,
因此它在 collections、
中有很多 records/documents
不简化我想根据时间戳获取records/documents。
- 我想获取过去 1 天内创建的所有记录。
- 我想获取过去 15 天内创建的所有记录。
- 我想获取过去 1 个月内创建的所有记录。
- 我想获取过去 1 年创建的所有记录。
我试过像这样写查询,但它不起作用。
var curDate = new Date().getTime();
SELECT * FROM r WHERE r._ts >= @curDate AND r.studentId=@studentId
new Date().getTime()
docs 说
A number representing the milliseconds elapsed between 1 January 1970 00:00:00 UTC and the given date.
_ts
docs 说
a number representing the number of elapsed seconds since January 1, 1970
所以你比较的是毫秒和秒,这不会产生任何结果。
您需要先将第一个值除以 1000,然后再将其与第二个值进行比较。当然,您需要减去 1 天、1 个月或您需要的任何值,例如
var twoDaysAgo = new Date();
twoDaysAgo.setDate(d.getDate() - 2);
var threshold = twoDaysAgo.getTime() / 1000;
SELECT * FROM r WHERE r._ts >= @threshold AND r.studentId=@studentId
我一直在使用 documentdb 数据库,
因此它在 collections、
中有很多 records/documents不简化我想根据时间戳获取records/documents。
- 我想获取过去 1 天内创建的所有记录。
- 我想获取过去 15 天内创建的所有记录。
- 我想获取过去 1 个月内创建的所有记录。
- 我想获取过去 1 年创建的所有记录。
我试过像这样写查询,但它不起作用。
var curDate = new Date().getTime();
SELECT * FROM r WHERE r._ts >= @curDate AND r.studentId=@studentId
new Date().getTime()
docs 说
A number representing the milliseconds elapsed between 1 January 1970 00:00:00 UTC and the given date.
_ts
docs 说
a number representing the number of elapsed seconds since January 1, 1970
所以你比较的是毫秒和秒,这不会产生任何结果。
您需要先将第一个值除以 1000,然后再将其与第二个值进行比较。当然,您需要减去 1 天、1 个月或您需要的任何值,例如
var twoDaysAgo = new Date();
twoDaysAgo.setDate(d.getDate() - 2);
var threshold = twoDaysAgo.getTime() / 1000;
SELECT * FROM r WHERE r._ts >= @threshold AND r.studentId=@studentId