我的文档是这样的:
{
"data": {
"eventId": "20161029125458-df-d",
"name": "first",
"purpose": "test",
"location": "yokohama",
"dateArray": [],
"attendees": [
{
"attendeeId": "2016102973634-df",
"attendeeName": "lakshman",
"personalizedDateSelection": {}
},
{
"attendeeId": "2016102973634-tyyu",
"attendeeName": "diwaakar",
"personalizedDateSelection": {}
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
比如说,我需要使用attendeeId: 2016102973634-df更新参与者JSON 数组。我尝试了很多使用update 和 condition expression 的方法,但没有成功。
这是我的尝试:
const params = {
TableName: "event",
Key: {
"eventId": eventId
},
UpdateExpression: "SET attendees[???] = ",
ConditionExpression: attendees.attendeeId = "2016102973634-df",
ExpressionAttributeValues: {
":attendee" : attendeeList
},
ReturnValues: "ALL_NEW"
};
dynamo.update(params, (err, data) => …Run Code Online (Sandbox Code Playgroud) arrays json amazon-web-services amazon-dynamodb dynamo-local
考虑DynamoDB表中的以下文档项/语法:
{
"id": "0f00b15e-83ee-4340-99ea-6cb890830d96",
"name": "region-1",
"controllers": [
{
"id": "93014cf0-bb05-4fbb-9466-d56ff51b1d22",
"routes": [
{
"direction": "N",
"cars": 0,
"sensors": [
{
"id": "e82c45a3-d356-41e4-977e-f7ec947aad46",
"light": true,
},
{
"id": "78a6883e-1ced-4727-9c94-2154e0eb6139",
}
]
}
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
我的目标是更新此JSON表示形式的单个属性,在本例中为cars。
我知道所有传感器ID。因此,达到该属性的最简单方法是在数组中找到具有带有任何id的传感器的路线。找到该传感器后,Dynamo应该知道他必须更新routes数组中的哪个对象。但是,如果不拒绝我的条件,我将无法运行此代码。
在这种情况下,请更新属性汽车,其中路线的ID为e82c45a3-d356-41e4-977e-f7ec947aad46或的传感器78a6883e-1ced-4727-9c94-2154e0eb6139。
var params = {
TableName: table,
Key:{
"id": "0f00b15e-83ee-4340-99ea-6cb890830d96",
"name": "region-1"
},
UpdateExpression: "set controllers.intersections.routes.cars = :c",
ConditionExpression: ""controllers.intersections.routes.sensors.id = :s",
ExpressionAttributeValues:{
":c": 1,
":s": "e82c45a3-d356-41e4-977e-f7ec947aad46"
},
ReturnValues:"UPDATED_NEW"
};
docClient.update(params, …Run Code Online (Sandbox Code Playgroud)