如何删除 dynamodb / python 中嵌套的 JSON 属性
How to delete nested JSON attribute in dynamodb / python
我有一个简单的 dynamodb 数据库,它使用“League”作为分区键,“Team”作为排序键,将所有名册数据存储在使用 JSON 格式的“Players”属性字段下。我想访问和删除特定的播放器(在这种情况下是 Larry Bird 或 Jayson Tatum),但是,我无法正确访问架构以删除特定的键和值,特别是考虑到 Players.Jayson Tatum 不会工作因为它是两个不同的词。到目前为止,这是函数的基本框架代码:
def lambda_handler(event, context):
newLeague = None
newTeam = None
newPlayer = None
statusCode = 200
if checkKey(event, 'League'):
newLeague = event['League']
if checkKey(event, 'Team'):
newTeam = event['Team']
if checkKey(event, 'Player'):
newPlayer = event['Player']
if newLeague != None and newTeam != None and newPlayer != None:
retrievedData = table.delete_item(
Key = {
'League': newLeague,
'Team': newTeam,
}
)
Dynamodb 中的数据库布局供参考
删除 DynamoDB 中的嵌套项可以通过使用 update_item
-方法中的 REMOVE
-表达式来完成:
client.update_item(
TableName=table_name,
Key={"League": {"S": "NBA"}, "Team": {"S": "Celtics"}},
UpdateExpression="REMOVE Players.#p",
ExpressionAttributeNames={"#p": "Larry Bird"},
)
请注意 ExpressionAttributeNames
,以解决玩家名称中包含 space 的事实。
以类似的方式添加玩家:
client.update_item(
TableName=table_name,
Key={"League": {"S": "NBA"}, "Team": {"S": "Celtics"}},
UpdateExpression="SET Players.#p = :v",
ExpressionAttributeNames={"#p": "Larry Bird"},
ExpressionAttributeValues={":v": {"M": {..}}}
)
我有一个简单的 dynamodb 数据库,它使用“League”作为分区键,“Team”作为排序键,将所有名册数据存储在使用 JSON 格式的“Players”属性字段下。我想访问和删除特定的播放器(在这种情况下是 Larry Bird 或 Jayson Tatum),但是,我无法正确访问架构以删除特定的键和值,特别是考虑到 Players.Jayson Tatum 不会工作因为它是两个不同的词。到目前为止,这是函数的基本框架代码:
def lambda_handler(event, context):
newLeague = None
newTeam = None
newPlayer = None
statusCode = 200
if checkKey(event, 'League'):
newLeague = event['League']
if checkKey(event, 'Team'):
newTeam = event['Team']
if checkKey(event, 'Player'):
newPlayer = event['Player']
if newLeague != None and newTeam != None and newPlayer != None:
retrievedData = table.delete_item(
Key = {
'League': newLeague,
'Team': newTeam,
}
)
Dynamodb 中的数据库布局供参考
删除 DynamoDB 中的嵌套项可以通过使用 update_item
-方法中的 REMOVE
-表达式来完成:
client.update_item(
TableName=table_name,
Key={"League": {"S": "NBA"}, "Team": {"S": "Celtics"}},
UpdateExpression="REMOVE Players.#p",
ExpressionAttributeNames={"#p": "Larry Bird"},
)
请注意 ExpressionAttributeNames
,以解决玩家名称中包含 space 的事实。
以类似的方式添加玩家:
client.update_item(
TableName=table_name,
Key={"League": {"S": "NBA"}, "Team": {"S": "Celtics"}},
UpdateExpression="SET Players.#p = :v",
ExpressionAttributeNames={"#p": "Larry Bird"},
ExpressionAttributeValues={":v": {"M": {..}}}
)