Aws lambda 如何在嵌套映射DynamoDb中进行更新

Aws lambda 如何在嵌套映射DynamoDb中进行更新,aws-lambda,Aws Lambda,我有一个名为cart的DDB表,我的订单列的格式如下 "order": { "PRD_SHOES_048": { "price": "40.99", "productId": "PRD_SHOES_048", "quantity": "1" } } 当我尝试向订单添加新项目时,现有值将被更新,并且表中只有新值。我需要这个专栏看起来像这样 "order": { "PRD_SHOES_048": { "price": "4

我有一个名为cart的DDB表,我的订单列的格式如下

"order": {
    "PRD_SHOES_048": {
      "price": "40.99",
      "productId": "PRD_SHOES_048",
      "quantity": "1"
    }
  }
当我尝试向订单添加新项目时,现有值将被更新,并且表中只有新值。我需要这个专栏看起来像这样

"order": {
    "PRD_SHOES_048": {
      "price": "40.99",
      "productId": "PRD_SHOES_048",
      "quantity": "1"
    },
 "PRD_TOP_047": {
      "price": "40.99",
      "productId": "PRD_TOP_047",
      "quantity": "1"
    }
  }

我使用的是节点js中编写的lambda函数。

您应该在JSON中添加一个
items
属性。按照现在的方式,很难跟踪所有的项目。您的订单对象应如下所示:

{
    "order": {
        "items": [
            {
                "id": "PRD_SHOES_048",
                "price": "40.99",
                "productId": "PRD_SHOES_048",
                "quantity": "1"
            },
            {
                "id": "PRD_TOP_047",
                "price": "40.99",
                "productId": "PRD_TOP_047",
                "quantity": "1"
            }
        ]
    }
}
order.items.push(myNewItem)
// save in DynamoDB...
现在,您可以轻松地在订单中添加新项目。只需获取订单并按如下方式推送新项目:

{
    "order": {
        "items": [
            {
                "id": "PRD_SHOES_048",
                "price": "40.99",
                "productId": "PRD_SHOES_048",
                "quantity": "1"
            },
            {
                "id": "PRD_TOP_047",
                "price": "40.99",
                "productId": "PRD_TOP_047",
                "quantity": "1"
            }
        ]
    }
}
order.items.push(myNewItem)
// save in DynamoDB...

您应该在JSON中添加
items
属性。按照现在的方式,很难跟踪所有的项目。您的订单对象应如下所示:

{
    "order": {
        "items": [
            {
                "id": "PRD_SHOES_048",
                "price": "40.99",
                "productId": "PRD_SHOES_048",
                "quantity": "1"
            },
            {
                "id": "PRD_TOP_047",
                "price": "40.99",
                "productId": "PRD_TOP_047",
                "quantity": "1"
            }
        ]
    }
}
order.items.push(myNewItem)
// save in DynamoDB...
现在,您可以轻松地在订单中添加新项目。只需获取订单并按如下方式推送新项目:

{
    "order": {
        "items": [
            {
                "id": "PRD_SHOES_048",
                "price": "40.99",
                "productId": "PRD_SHOES_048",
                "quantity": "1"
            },
            {
                "id": "PRD_TOP_047",
                "price": "40.99",
                "productId": "PRD_TOP_047",
                "quantity": "1"
            }
        ]
    }
}
order.items.push(myNewItem)
// save in DynamoDB...

请共享您的codevar docClient=new AWS.DynamoDB.DocumentClient();var table=“电子购物车”;var输入=事件;var params={TableName:table,Item:input};log(“Params”+JSON.stringify(Params));log(“添加新项…”);docClient.put(params,function(err,data){if(err){callback(null,{“statusCode”:400,“message”:JSON.stringify(err.message)};}否则{callback(null,{“statusCode”:200,“message”:“Success”,“cartId”:ts});}请共享您的代码var docClient=new AWS.DynamoDB.DocumentClient();var table=“e_cart”;var input=event;var params={TableName:table,Item:input};console.log(“params”+JSON.stringify(params));console.log(“添加新项…”);docClient.put(params,函数(err,数据){if(err){){callback(null,{“statusCode”:400,“message”:JSON.stringify(err.message)};}否则{回调(null,{“statusCode”:200,“message”:“Success”,“cartId”:ts});}