pedro.olimpio
pedro.olimpio

Reputation: 1498

DynamoDB - Scan not returning all items

I am facing some troubles when scanning my table in DynamoDB in my Node.js function.

When I scanning at the function I got 2 rows, but when scanning at AWS console I am getting 3 rows.

Follow below the code:

utilsAWS scan function:

scanDocuments(params) {
    return new Promise((resolve, reject) => {
      var docClient = new this.AWS.DynamoDB.DocumentClient();

      console.log(`Querying ${params.TableName}...`);

      docClient.scan(params, function (err, data) {
        if (err) {
          console.error(
            "Unable to query. Error:",
            JSON.stringify(err, null, 2)
          );
          return reject(err);
        } else {
          console.log("Query succeeded.");
          return resolve(data.Items);
        }
      });
    });
  }

//consuming scan function

var params = {
    TableName: 'minha-redacao-redacoes',
    FilterExpression: "#e = :cpfAluno",
    ExpressionAttributeNames: {
      "#e": "cpfAluno",
    },
    ExpressionAttributeValues: {
      ":cpfAluno": `8509754....`
    },
  };
  try {
    const res = await utilsAWS.scanDocuments(params);

    console.log(res.length); //2

  } catch(err) {
    console.error(err);
  }

But look at the AWS console:

scan by console

The row id: f3ebb776-13eb-4395-884e-e81f23044ca1 is not found when scanning by the node.js function. To solve partially the problem I've created the f3ebb776-13eb-4395-884e-e81f23044ca1_manual document.

Any one knows a definitive solution to this issue?

Upvotes: 3

Views: 4310

Answers (1)

Tasos P.
Tasos P.

Reputation: 4114

Assuming that all 3 records actually satisfy the filtering criteria, your should check LastEvaluatedKey attribute of the response.

Despite the small number of items involved, there is no guaranty that a scan operation will return all matching elements in the first response. The contract is:

  • if: LastEvaluatedKey is empty, then you are on the last page of results
  • else: you need to repeat the scan operation with a parameter ExclusiveStartKey in order to resume the scan.

More info in the official docs.

Upvotes: 6

Related Questions