簡體   English   中英

如何在 DynamoDB 中為 map 添加新屬性?

[英]How to add new attributes to map in DynamoDB?

我的數據庫結構,

{
  "email":"example@mail.com",
  "products": {
    "product1":{
      "price":"$10",
      "details":"detail"
    },
    "product2":{
      "price":"$20",
      "details":"detail"
    }
  }
}

我想向“產品”添加新屬性 map 並期望 output 如下所示,

{
  "email":"example@mail.com",
  "products": {
    "product1":{
      "price":"$10",
      "details":"detail"
    },
    "product2":{
      "price":"$20",
      "details":"detail"
    },
    "product3":{
      "price":"$10",
      "details":"detail"
    }
  }
}

我正在使用 API 網關和 UpdateItem 操作。 這是我的映射模板,

{
    "TableName": "tableName",
    "Key": {
        "email": {
            "S": "$input.path('$.email')"
        }
    },
    "UpdateExpression": "SET #pr = :vals",
    "ExpressionAttributeNames": {
        "#pr": "products"
    },
    "ExpressionAttributeValues": {
        ":vals": {
            "M": {
                "$input.path('$.productId')": {
                    "M": {
                        "price": {
                            "N": "$input.path('$.price')"
                        },
                        "details": {
                            "S": "$input.path('$.details')"
                        }
                    }
                }
            }
        }
    },
    "ReturnValues": "NONE"
}

使用上面的模板將替換我的所有屬性。 實際output,

{
  "email":"example@mail.com",
  "products": {
    "product3":{
      "price":"$10",
      "details":"detail"
    }
  }
}

如何向 map 添加新屬性而不是替換它?

謝謝。

在您的請求中,您正在SET整個產品 map,但您只想添加一個嵌套的 map。

{
    "TableName": "tableName",
    "Key": {
        "email": {
            "S": "$input.path('$.email')"
        }
    },
    "UpdateExpression": "SET #pr.product3 = :vals",
    "ExpressionAttributeNames": {
        "#pr": "products"
    },
    "ExpressionAttributeValues": {
        ":vals": {
            "M": {
                "$input.path('$.productId')": {
                    "M": {
                        "price": {
                            "N": "$input.path('$.price')"
                        },
                        "details": {
                            "S": "$input.path('$.details')"
                        }
                    }
                }
            }
        }
    },
    "ReturnValues": "NONE"
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM