DynamoDB scan with multiple filters

0

I'm trying to do a DynamoDB Scan from a NodeJS lambda, and the results I'm getting are hugely different than when I enter the same filters explicitly in DynamoDB directly. I'm using the following code:

var params = {
        TableName : tbl,
        FilterExpression: "#uploadDate BETWEEN :date1 and :date2 AND #imageSeen = :notSeen",
        ExpressionAttributeValues : {
            ":date1" : StartDate,
            ":date2" : EndDate,
            ":notSeen"  : false
        },
        ExpressionAttributeNames: {
            "#uploadDate": "UploadDate",
            "#imageSeen" : "ImageSeen"
          }
      };

And then the scan:

scanWithParams: async(params) => {
        let res ={};
        try {
             res = await documentClient.scan(params).promise();
        }
        catch (err) {
            console.log('[scanWithParams] : error scanning' , err);
        }
        return res.Items;
    }   

Both dates are strings, and ImageSeen is a boolean. When I run this directly, I get 1800 results back, while via the above code I only get 36.

Any help is greatly appreciated.

Update: - Here's a picture of doing it directly -- Does my params match this, the way I wrote it?Enter image description here

已提問 1 年前檢視次數 904 次
2 個答案
1
已接受的答案

The DynamoDB Web Console auto-paginates on your behalf, and sets a limit of 300, so it will always return 300 items if they exist:

{
    "TableName":"YourTable",
    "ReturnConsumedCapacity":"TOTAL",
    "Limit":300,
    "Select":"ALL_ATTRIBUTES",
    "FilterExpression":"YourFilterExpression"
}

In order for you to achieve the same results, you will need to include pagination in your code. You can read more here

myItems = []
scanWithParams: async(params) => {
        let res ={};
        try {
             res = await documentClient.scan(params).promise();
             myItems  = [...myItems, ...res['Items']];
             if (data.LastEvaluatedKey) {
                     params.ExclusiveStartKey = res.LastEvaluatedKey;
                     return await getAllData(params);
            }
        }
        catch (err) {
            console.log('error scanning' , err);
        }
        return myItems;
    }
profile pictureAWS
專家
已回答 1 年前
profile pictureAWS
專家
已審閱 1 年前
0

Is it possible that the scan operation is returning more than 1MB of data? That is the limit per scan operation.

If by "run this directly" you mean in the console, what you're seeing is the console automatically calling scan multiple times to get additional data. This is indicated in the return data if the LastEvaluatedKey parameter is present. https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.Pagination.html

profile pictureAWS
專家
已回答 1 年前
  • Hi .. thank you. This might be my problem. So what would be the way to get all of that data, from my lambda, if it exceeds 1MB?

  • Actually, it does paginate, but the first page I get in the console contains 300 items, not 36.

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南