在Dynamodb时间戳中,compare(Reference:https://www.abhayachauhan.com/2017/12/how-to-store-dates-or-timestamps-in-dynamodb/)未能按预期工作。
具有一行值的Dynamod db属性:
id :“ 123”,
triggerDate :“ 2019-06-07T15:10:18.252Z”
不受理案件:
假设在“ triggerDate”时间之后执行的满足“ triggerDate”的代码小于“ currentTime”(triggerDate < :t
)。
var currentTime = new Date();
currentTime = JSON.stringify(currentTime);
console.log("currentTime:", currentTime);
let params = {
TableName: TABLE_NAME,
KeyConditionExpression: "id = :i AND triggerDate < :t",
ExpressionAttributeValues: {
":t": { S: currentTime },
":i": { S: "123"}
}
};
const data = await dynamodb.query(params).promise();
console.log(JSON.stringify(data));
输出:
2019-06-08T05:09:02.782Z c4f65ba2-3d3d-43f6-833f-51735bbdcda5 INFO currentTime: "2019-06-08T05:09:02.781Z"
2019-06-08T05:09:03.480Z c4f65ba2-3d3d-43f6-833f-51735bbdcda5 INFO {"Items":[],"Count":0,"ScannedCount":0}
即使“ triggerDate”小于“ currentTime”(triggerDate < :t
),dynamodb也不会返回结果。
工作情况:其中对currentTime进行了硬编码,而不是从new Date()
读取以符合“ triggerDate”的情况小于“ currentTime”。
let currentTime = "2019-06-08T05:09:02.781Z";
let params = {
TableName: TABLE_NAME,
KeyConditionExpression: "id = :i AND triggerDate < :t",
ExpressionAttributeValues: {
":t": { S: currentTime },
":i": { S: "123"}
}
};
const data = await dynamodb.query(params).promise();
console.log(JSON.stringify(data));
输出:
2019-06-08T04:35:54.775Z 60f87d6c-0ffb-477a-95ab-cfaec8025d53 INFO currentTime: "2019-06-08T05:09:02.781Z"
2019-06-08T04:35:55.594Z 60f87d6c-0ffb-477a-95ab-cfaec8025d53 INFO {"Items":[{"id":{"S":"124"},"triggerDate":{"S":"2019-06-07T15:53:54.107Z"}}],"Count":1,"ScannedCount":1}
Dynamodb将在此处返回结果。
为什么在无效案例中,即使“ triggerDate”小于“ currentTime”,dynamodb也不返回任何数据?
我有什么想念的吗?
谁能解释这种行为?
答案 0 :(得分:1)
使用JSON.stringify()将日期对象转换为字符串是一种错误的处理方式,很可能会导致格式错误。相反,您应该使用Date.toISOString() function将日期对象转换为字符串。
currentTime = currentTime.toISOString();