简体   繁体   English

nodejs DynamoDB 按映射项扫描

[英]nodejs DynamoDB Scan by Mapped Items

I have a DynamoDB table where I would like to return all Items whose Review.ID mapped value is equal to 123.我有一个 DynamoDB 表,我想在其中返回 Review.ID 映射值等于 123 的所有项目。

Item: {
  id: 1,
  review: {
    Id: 123,
    step1: 456,
    step2: 789,
    step3: 1234,
    },
  // Add more items here
},
Item: {
  id: 2,
  review: {
    Id: 123,
    step1: 999,
    step2: 888,
    step3: 777,
    },
  // Add more items here
},

Ideal Return Example:理想回报示例:

id: 1,
review: {
 - id: 123,
 - step: 456,
 - step2: 789,
 - step3: 1234,
}
id: 2,
review: {
  - id: 123,
 - step: 999,
 - step2: 888,
 - step3: 777,
}

This is my current code in NodeJS这是我在 NodeJS 中的当前代码

exports.handler = async (event, context, callback) => {

 const params = {
    FilterExpression: "review.Id = :review",
    ExpressionAttributeValues: {
        ":review": 123,
    },
  TableName: "sometable"
 };


   let promise = dynamoDb.scan(params).promise();
   let result = await promise;
   let data = result.Items;
    if (result.LastEvaluatedKey) {
        params.ExclusiveStartKey = result.LastEvaluatedKey;
        data = data.concat(await (params));
    }


    // create a response
      const response = {
      statusCode: 200,
      body: JSON.stringify(data),
    };
    callback(null, response);

};

Running this code I get an empty result [].运行此代码,我得到一个空结果 []。

Any help on this would be greatly appreciated.对此的任何帮助将不胜感激。

You haven't shown all your code but I am going to suggest that the problem is that your dynamoDb client is the low-level DynamoDB Client rather than the high-level Document Client .您尚未显示所有代码,但我建议问题在于您的dynamoDb客户端是低级DynamoDB Client而不是高级Document Client They use different ways of supplying attribute values.他们使用不同的方式提供属性值。 You're using the low-level DynamoDB client but indicating attribute values as if you're using the high-level Document Client.您正在使用低级别 DynamoDB 客户端,但指示属性值就像您正在使用高级文档客户端一样。

Here's an example that contrasts the two client approaches:这是一个对比两种客户端方法的示例:

const AWS = require('aws-sdk');
AWS.config.update({ region: 'us-east-1' });

// High-level Document Client
const dc = new AWS.DynamoDB.DocumentClient();

// High-level scan parameters for Document Client
const paramsDC = {
  FilterExpression: "review.Id = :review",
  ExpressionAttributeValues: {
    ":review": 123,
  },
  TableName: "sometable"
};

// Low-level DynamoDB Client
const db = new AWS.DynamoDB();

// Low-level scan parameters for DynamoDB Client
const paramsDB = {
  FilterExpression: "review.Id = :review",
  ExpressionAttributeValues: {
    ":review": { "N": "123" },
  },
  TableName: "sometable"
};

(async() => {
  // High-level scan
  const resultDC = await dc.scan(paramsDC).promise();
  console.log('DC Items:', JSON.stringify(resultDC.Items));

  // Low-level scan
  const resultDB = await db.scan(paramsDB).promise();
  console.log('DB Items:', JSON.stringify(resultDB.Items));
})();

This results in:这导致:

DC Items: [{"review":{"step":999,"id":123},"id":2},{"review":{"step":456,"id":123},"id":1}]
DB Items: [{"review":{"M":{"step":{"N":"999"},"id":{"N":"123"}}},"id":{"N":"2"}},{"review":{"M":{"step":{"N":"456"},"id":{"N":"123"}}},"id":{"N":"1"}}]

Note that the low-level DynamoDB Client response looks a little different but is actually just a different (unmarshalled) representation of the same results.请注意,低级别 DynamoDB 客户端响应看起来有些不同,但实际上只是相同结果的不同(未编组)表示。 It includes "id":{"N":"1"} which says that id is a number with value 1 where the Document Client shows "id":1 .它包括"id":{"N":"1"} ,它表示id是一个值为1的数字,其中 Document Client 显示"id":1 The low-level results includes "M": {... } which are maps, while the high-level Document Client unmarshalls these to JavaScript objects for you automatically.低级结果包括"M": {... }这是映射,而高级文档客户端会自动为您将这些解组为 JavaScript 对象。

Bottom-line:底线:

  • the Document Client is newer文档客户端较新
  • the Document Client marshalls and unmarshalls data to/from JavaScript native objects文档客户端对 JavaScript 本机对象的数据进行编组和解组
  • use the Document Client使用文档客户端

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM