更新表达式updateExpression
Posted landen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了更新表达式updateExpression相关的知识,希望对你有一定的参考价值。
主题:
- set 修改属性
case ‘PUT‘: {
var table = ‘book_table‘, hash = ‘book‘, id = "05d101be-d5e8-43ec-8eb6-5530e21af83e";
var params = {
TableName: table,
Key: {
"hash": hash,
"id": id
},
UpdateExpression: "SET #p = :p, #sn = :n, #c = #c + :c",
ExpressionAttributeNames: {
"#p": "price",
"#sn": "sn",
"#c": "count"
},
ExpressionAttributeValues: {
":p": 100.99,
":n": "100-100-010",
":c": 1
},
ReturnValues: "UPDATED_NEW"
};
await docClient.update( params ).promise().then(
( success ) => {
response.body = JSON.stringify({ "success:": success })
}
).catch(
( err ) => {
console.log(err);
response.statusCode = err.statusCode;
response.body = JSON.stringify({
code: err.code,
message: err.message
})
}
);
callback( null, response );
break;
};
程序运行成功返回如下:
{ "success:": { "Attributes": { "count": 9, "price": 100.99, "sn": "100-100-010" } } }
- 添加列表和映射
case ‘POST‘: {
var table = "book_table", hash = "book";
var params = {
TableName: table,
Item: {
"hash": hash,
"id": uuid(),
"booktype":[‘青春文学‘, ‘历史典故‘]
}
};
await docClient.put( params ).promise().then(
( data ) => {
response.body = JSON.stringify({"success": data})
}
).catch(
( err ) =>{
response = {
statusCode: err.statusCode,
body: JSON.stringify({
code: err.code,
message: err.message
})
}
}
)
callback( null, response );
break;
}
程序运行成功后返回:
{ "success": {} }
- 将元素添加到列表:
case ‘PUT‘: {
var table = ‘book_table‘, hash = ‘book‘, id = "bee1bea0-4b6a-4fbc-8cf0-8c030519c909";
var params = {
TableName: table,
Key: {
"hash": hash,
"id": id
},
UpdateExpression: "SET #type = list_append(#type, :t)",
ExpressionAttributeNames: {
"#type": "booktype"
},
ExpressionAttributeValues: {
":t": ["读者杂志","最小说","科技畅想"]
},
};
await docClient.put( params ).promise().then(
( data ) => {
response.body = JSON.stringify({"success": data})
}
).catch(
( err ) =>{
response = {
statusCode: err.statusCode,
body: JSON.stringify({
code: err.code,
message: err.message
})
}
}
)
callback( null, response );
break;
}
程序运行成功后返回:
{ "success:": { "Attributes": { "booktype": [ "青春文学", "历史典故", "读者杂志", "最小说", "科技畅想", ], "hash": "book", "id": "556433cf-37b6-4e12-b513-9c261fd279f7" } } }
- 添加嵌套映射属性
case ‘POST‘: {
var table = "book_table", hash = "book";
var params = {
TableName: table,
Item: {
"hash": hash,
"id": uuid(),
"type": {
"list1": ["武侠小说", "社科图书"],
"list2": ["儿童读物", "漫画书"],
"list3": ["法律", "经济"]
}
}
};
await docClient.put( params ).promise().then(
( data ) => {
response.body = JSON.stringify({"success": data})
}
).catch(
( err ) =>{
response = {
statusCode: err.statusCode,
body: JSON.stringify({
code: err.code,
message: err.message
})
}
}
)
callback( null, response );
break;
}
case ‘PUT‘: {
var table = ‘book_table‘, hash = ‘book‘, id = "bee1bea0-4b6a-4fbc-8cf0-8c030519c909";
var params = {
TableName: table,
Key: {
"hash": hash,
"id": id
},
UpdateExpression: "SET #t.#l1 = :b1, #t.#l3 = :b3",
ExpressionAttributeNames: {
"#t": "type",
"#l1": "list1",
"#l3": "list3"
},
ExpressionAttributeValues: {
":b1": ["军事","艺术"],
":b3": ["社会","科学"]
},
ReturnValues: "ALL_NEW"
};
await docClient.update( params ).promise().then(
( success ) => {
response.body = JSON.stringify({ "success:": success })
}
).catch(
( err ) => {
console.log(err);
response.statusCode = err.statusCode;
response.body = JSON.stringify({
code: err.code,
message: err.message
})
}
);
callback( null, response );
break;
};
程序运行成功后返回:
{ "success:": { "Attributes": { "type": { "list1": [ "军事", "艺术" ], "list3": [ "社会", "科学" ], "list2": [ "儿童读物", "漫画书" ] }, "hash": "book", "id": "bee1bea0-4b6a-4fbc-8cf0-8c030519c909" } } }
- 对数值属性进行加减(对现有数值属性执行加减运算。为此,请使用
+
(加号)和-
(减号)运算符。)
case ‘POST‘: {
var table = "book_table", hash = "book";
var params = {
TableName: table,
Item: {
"hash": hash,
"id": uuid(),
"name": "三重门",
"author": "韩寒",
"type": "情感其他",
"sn": "100-100-008",
"price": 103.50,
"date": "2010-01-01",
"count": 7,
"description": "本书通过少年林雨翔的视角,向读者揭示了真实的高中生的生活,体现了学生式的思考、困惑和梦想。"
}
};
console.log( params.Item.count );
await docClient.put( params ).promise().then(
( data ) => {
response.body = JSON.stringify({"success": data})
}
).catch(
( err ) =>{
response = {
statusCode: err.statusCode,
body: JSON.stringify({
code: err.code,
message: err.message
})
}
}
)
callback( null, response );
break;
}
case ‘PUT‘: {
var table = ‘book_table‘, hash = ‘book‘, id = ‘4b99e00f-9f57-41f7-80de-d581e8c84522‘;
var params = {
TableName: table,
Key: {
"hash": hash,
"id": id
},
UpdateExpression: "SET #p = :p, #sn = :n, #c = #c + :c",
ExpressionAttributeNames: {
"#p": "price",
"#sn": "sn",
"#c": "count"
},
ExpressionAttributeValues: {
":p": 100.99,
":n": "100-100-010",
":c": 1
},
ReturnValues: "ALL_NEW"
};
await docClient.update( params ).promise().then(
( success ) => {
response.body = JSON.stringify({ "success:": success })
}
).catch(
( err ) => {
console.log(err);
response.statusCode = err.statusCode;
response.body = JSON.stringify({
code: err.code,
message: err.message
})
}
);
callback( null, response );
break;
};
程序运行成功后返回:
{ "success:": { "Attributes": { "date": "2010-01-01", "author": "韩寒", "price": 99.99, "count": 8, "name": "三重门", "description": "本书通过少年林雨翔的视角,向读者揭示了真实的高中生的生活,体现了学生式的思考、困惑和梦想。", "id": "4b99e00f-9f57-41f7-80de-d581e8c84522", "sn": "100-100-100", "type": "情感其他", "hash": "book" } } }
- 将元素附加到列表
case ‘PUT‘: {
var table = ‘book_table‘, hash = ‘book‘, id = ‘4b99e00f-9f57-41f7-80de-d581e8c84522‘;
var params = {
TableName: table,
Key: {
"hash": hash,
"id": id
},
UpdateExpression: "SET #type = list_append(#type, :t)",
ExpressionAttributeNames: {
"#type": "booktype"
},
ExpressionAttributeValues: {
":t": ["穿越时空", "玄幻小说"]
},
ReturnValues: "ALL_NEW"
};
await docClient.update( params ).promise().then(
( success ) => {
response.body = JSON.stringify({ "success:": success })
}
).catch(
( err ) => {
console.log(err);
response.statusCode = err.statusCode;
response.body = JSON.stringify({
code: err.code,
message: err.message
})
}
);
callback( null, response );
break;
};
程序运行成功后返回:
{ "success:": { "Attributes": { "booktype": [ "青春文学", "历史典故", "读者杂志", "最小说", "科技畅想", "穿越时空", "玄幻小说" ], "hash": "book", "id": "556433cf-37b6-4e12-b513-9c261fd279f7" } } }
- 防止覆盖现有属性(设置项目的 Price,但仅当项目还没有 Price 属性时设置。(如果 Price 已存在,则不执行任何操作。))
case ‘PUT‘: {
var table = ‘book_table‘, hash = ‘book‘, id = ‘4b99e00f-9f57-41f7-80de-d581e8c84522‘;
var params = {
TableName: table,
Key: {
"hash": hash,
"id": id
},
UpdateExpression: "SET price = if_not_exists(price, :p)",
ExpressionAttributeValues: {
":p": 100
},
ReturnValues: "ALL_NEW"
};
await docClient.update( params ).promise().then(
( success ) => {
response.body = JSON.stringify({ "success:": success })
}
).catch(
( err ) => {
console.log(err);
response.statusCode = err.statusCode;
response.body = JSON.stringify({
code: err.code,
message: err.message
})
}
);
callback( null, response );
break;
};
程序运行成功后返回:
{ "success:": { "Attributes": { "booktype": [ "青春文学", "历史典故", "读者杂志", "最小说", "科技畅想", "穿越时空", "玄幻小说" ], "id": "556433cf-37b6-4e12-b513-9c261fd279f7", "price": 100, "hash": "book" } } }
- REMOVE::从项目中删除属性(如果属性不存在,则不会执行任何操作。)
case ‘PUT‘: {
var table = ‘book_table‘, hash = ‘book‘, id = ‘4b99e00f-9f57-41f7-80de-d581e8c84522‘;
var params = {
TableName: table,
Key: {
"hash": hash,
"id": id
},
UpdateExpression: "REMOVE booktype[6]",
ReturnValues: "ALL_NEW"
};
await docClient.update( params ).promise().then(
( success ) => {
response.body = JSON.stringify({ "success:": success })
}
).catch(
( err ) => {
console.log(err);
response.statusCode = err.statusCode;
response.body = JSON.stringify({
code: err.code,
message: err.message
})
}
);
callback( null, response );
break;
};
程序运行成功后返回:
{ "success:": { "Attributes": { "booktype": [ "青春文学", "历史典故", "读者杂志", "最小说", "科技畅想", "穿越时空", ], "id": "556433cf-37b6-4e12-b513-9c261fd279f7", "price": 100, "hash": "book" } } }
注: ALL_NEW 将导致 DynamoDB 按更新后的情况返回项目;UPDATED_NEW仅将DynamoDB 按更新后的部分情况返回项目。
本文为原创,还望指教!
若有疑问,可在下方留言,也可自读AWS官方文档——DynamoDB: https://docs.aws.amazon.com/zh_cn/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html
以上是关于更新表达式updateExpression的主要内容,如果未能解决你的问题,请参考以下文章