問題描述
我想掃描過去 7 天的所有項目,所以我要做的是生成 7 天前的時間戳并過濾大于該值的時間戳.但此掃描返回了一些結果.
I want to scan all items for last 7 days, so what I do is I generate timestamp for 7 days back and filter for timestamp greater than that value. But this scan is returning a few results.
請參閱以下 Javascript:
See the following Javascript:
const daysBack = (days) => {
let date = new Date();
date.setDate(date.getDate() - days);
return date.getTime() ;
}
const params = {
TableName: process.env.DYNAMODB_TABLE,
FilterExpression: "#ts > :z",
ExpressionAttributeNames:{
"#ts": "timestamp"
},
ExpressionAttributeValues: {
":z": daysBack(7)
},
};
dynamoDb.scan(params, (error, result) => {
// ...
}
推薦答案
這是因為在 SCAN 操作
上 dynamoDb 只會發送 最多 1mb 的數據
.如果您想要的記錄大小超過 1mb,則會自動分頁.
It is because on a SCAN operation
dynamoDb will only send data upto 1mb only
. If the records you want are of size more than 1mb automatically pagination happens.
如果你記錄你的結果,那么你會發現一個名為 LastEvaluatedKey
的屬性如果此屬性存在,那么您將不得不再次調用以獲取剩余數據.此調用必須遞歸實現,并且您必須在 LastEvaluatedKey
屬性不存在時停止它.
If you log your Result then you will find an attribute called LastEvaluatedKey
if this attribute is present then you will have to make another call to get the remaining data. This call has to be implemented recursively and you have to stop it when LastEvaluatedKey
attribute is not present.
讓我們看看這個例子,項目數據被遞歸獲取,整個數據被附加到數組中,然后發送.
Lets see this example where project data is been fetched recursively and the whole data is appended in the array and then send.
let getFromDb = function (params, callback) {
params.ConsistentRead = true;
let projectCollection = [];
dynamodbclient.scan(params, onQuery);
function onQuery(err, data) {
const methodName = 'onQuery';
if (err) {
callback(err);
log.error(err, {
class: className,
func: methodName
});
} else {
for (let i = constant.LENGTH_ZERO; i < data.Items.length; i++) {
projectCollection.push(data.Items[i]);
}
if (typeof data.LastEvaluatedKey !== 'undefined') {
params.ExclusiveStartKey = data.LastEvaluatedKey;
dynamodbclient.scan(params, onQuery);
} else {
callback(err, projectCollection); //recursive call
}
}
}
};
這篇關于Dynamodb 過濾器表達式不返回所有結果的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!