DynamoDB - 扫描不返回所有项目

DynamoDB - Scan not returning all items

我在 node.js 函数中扫描 DynamoDB 中的 table 时遇到了一些问题。

当我在函数中扫描时,我得到了 2 行,但是在 AWS 控制台中扫描时,我得到了 3 行。

按照下面的代码:

utilsAWS 扫描函数:

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);
        }
      });
    });
  }

//消耗扫描函数

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);
  }

但是看看 AWS 控制台:

node.js 函数扫描时未找到行 ID:f3ebb776-13eb-4395-884e-e81f23044ca1。为了部分解决问题,我创建了 f3ebb776-13eb-4395-884e-e81f23044ca1_manual 文档。

有人知道这个问题的最终解决方案吗?

假设所有 3 条记录实际上都满足过滤条件,您应该检查响应的 LastEvaluatedKey 属性。

尽管涉及的项目数量很少,但无法保证 scan 操作将 return 第一个响应中的所有匹配元素。合同是:

  • 如果:LastEvaluatedKey 为空,则您位于结果的最后
  • else: 您需要使用参数ExclusiveStartKey重复扫描操作才能恢复扫描。

更多信息in the official docs