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

demandé il y a un an963 vues
2 réponses
1
Réponse acceptée

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
EXPERT
répondu il y a un an
profile pictureAWS
EXPERT
vérifié il y a un an
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
EXPERT
répondu il y a un an
  • 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.

Vous n'êtes pas connecté. Se connecter pour publier une réponse.

Une bonne réponse répond clairement à la question, contient des commentaires constructifs et encourage le développement professionnel de la personne qui pose la question.

Instructions pour répondre aux questions