【问题标题】:How to query and map data from another table?如何从另一个表中查询和映射数据?
【发布时间】:2021-07-19 18:51:10
【问题描述】:

我有 DynamoDB 表订单、产品和用户。

在订单中我有prouctiduser_email

我想使用 productid 从订单表中获取每个产品详细信息。

async function getUserOrders(id) {
  let params = {
    TableName: table_name_orders,
    IndexName: "UserEmail",
    KeyConditionExpression: "#email = :v_email",
    ExpressionAttributeNames: {
      "#email": "user_email",
    },
    ExpressionAttributeValues: {
      ":v_email": id,
    },
  };

  let allData = await dynamodb.query(params).promise();

  return buildResponse(200, allData.Items);
}

在此我正在查询特定用户的所有订单,此订单的产品 id 指向产品我想获取每个订单的每个产品数据我如何获得这些数据。

有没有像 populate(Mongodb) 这样的函数用数据填充数组中的每个项目?

【问题讨论】:

标签: amazon-web-services aws-lambda amazon-dynamodb dynamodb-queries


【解决方案1】:

您可以使用 DynamoDB batchGetItem API - https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchGetItem.html

java 中的示例:-

//产品表hashKeys列表

列出idList;

// 使用 Java SDK 进行 BatchGet

Map 声明 = dynamoDBMapper.batchLoad(Collections.singletonMap(TableName.class, idList.stream().distinct() .map(i -> new KeyPair().withHashKey(i)) .collect(Collectors.toList()))).get("").stream() .map(object -> (TableName class) object).collect(Collectors.toMap(Claim::getClaimId, Function.identity() , (i1, i2) -> i2));

【讨论】:

  • 你能给我一份nodejs代码的sn-p吗?我现在被困在这个问题上我正在做的是循环每个查询从产品表中为每个 orderId 获取数据
猜你喜欢
  • 2019-08-02
  • 2021-10-30
  • 2021-02-10
  • 2019-07-03
  • 2022-09-27
  • 2011-12-04
  • 1970-01-01
  • 2020-04-02
  • 1970-01-01
相关资源
最近更新 更多