最近我正在使用 DynamoDB 构建一些 API。
在一个名为planet的表中,有一个属性是List of Maps(Array of Objects)的形式。基本上,属性看起来像这样。我将其称为buildingMap,因为它是许多建筑物的地图。
[
{
id: 'some id',
status: 'processing',
...
},
{
id: 'other id',
status: 'done',
...
}
]
Run Code Online (Sandbox Code Playgroud)
事情是,最初,我指出索引并直接更新数组中的该对象,就像使用这种 UpdateExpression 一样。
REMOVE buildingMap[2]
或像这样的 UpdateExpression
SET buildingMap[0].#status = :status
我认为这将使 DynamoDB 消耗的容量比我取出整个buildingMap,重写它,然后将整个buildingMap放回去要少。
像这样
// Get the item using DynamoDB.DocumentClient
const planet = await docClient.get({
TableName: 'Planet',
Key: { planetId: 'xxxxx' }
}).promise()
// Modify the buildingMap attribute
let buildingMap = planet.Item.buildingMap
buildingMap.splice(0, 1) // …Run Code Online (Sandbox Code Playgroud)