更新表达式updateExpression
主题:
- SET - 修改或添加项目属性
- REMOVE - 从项目中删除属性
- ADD - 更新数字和集合
- DELETE - 从集合中删除元素
- 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:从项目中删除属性(如果属性不存在,则不会执行任何操作。)
ps: DELETE不存在 "ALL_NEW"或”UPDATED_NEW“的属性
1 case 'PUT': { 2 3 var table = 'book_table', hash = 'book', id = '4b99e00f-9f57-41f7-80de-d581e8c84522'; 4 5 var params = { 6 TableName: table, 7 Key: { 8 "hash": hash, 9 "id": id 10 }, 11 12 UpdateExpression: "REMOVE booktype[7]", 13 ReturnValues: "ALL_NEW" 14 }; 15 16 await docClient.update( params ).promise().then( 17 ( success ) => { 18 response.body = JSON.stringify({ "success:": success }) 19 } 20 21 ).catch( 22 ( err ) => { 23 console.log(err); 24 response.statusCode = err.statusCode; 25 response.body = JSON.stringify({ 26 code: err.code, 27 message: err.message 28 }) 29 } 30 ); 31 callback( null, response ); 32 break; 33 };
程序运行成功后返回:
{"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
转载于:https://www.cnblogs.com/landen/p/9780768.html
更新表达式updateExpression相关推荐
- 【MongoDB】递归获取字段更新表达式,更新复杂数据类型对象
在实际更新Mongo对象时发现,原有的更新代码无法更新复杂的数据类型对象.恰好看到张占岭老师有对该方法做相关的改进,因此全抄了下来. 总的核心思想就是运用反射与递归,对对象属性一层一层挖掘下去,循环创 ...
- C++PrimerPlus 第五章 循环和关系表达式 - 5.1 for循环
C++PrimerPlus 第五章 循环和关系表达式 - 5.1 for循环 5.1 for循环 5.1.1 for循环的组成部分 5.1.1.1 表达式和语句 5.1.1.2 非表达式和语句 5.1 ...
- DynamoDB-条件表达式ConditionExpression
主题: 防止覆盖项目现有的项目 检查项目中的属性 有条件删除 有条件更新 比较运算符和函数引用 条件表达式的语法: condition-expression ::= operand com ...
- mongodb更新语句_MongoDB更新
mongodb更新语句 MongoDB update is used to update document in a collection. In last tutorial, we learned ...
- Qt实战项目:高精度表达式计算器Qt
高精度表达式计算器Qt 表达式计算器是一个由C++ with QT编写的应用程序.这个计算器旨在通过引入表达式计算的概念解决Windows默认计算器显示不直观的问题,提供了普通型计算器.科学型计算器的 ...
- java基础(适合零基础)持续更新
目录 java语言的概述 1.概述 2.Java语言特点 3.java的加载与执行 开始第一个java程序 1.安装JDK(这里安装JDK1.8) 2.安装文本编辑器Editplus 3.JDK目录介 ...
- mysql数据库更新_将低于总平均成绩的女生的成绩提高5分
将低于总平均成绩的女生的成绩提高5分 该更新需要用到 " update 表名 set 更新表达式 where 条件 " 语句. 根据题意,"女生"需要用到含有性 ...
- ABP 在 EntityFramework 中使用扩展批量更新时的异常
报错信息:Invalid Cast. The update expression must be of type MemberInitExpression. 直译结果:无效的.更新表达式必须是Memb ...
- js ...运算符_「 giao-js 」用js写一个js解释器
前言 在这篇文章中,我们将通过 JS 构建我们自己的 JS 解释器,用 JS 写 JS,这听起来很奇怪,尽管如此,这样做我们将更熟悉 JS,也可以学习 JS 引擎是如何工作的! 什么是解释器 (Int ...
最新文章
- 如何在CPU上优化GEMM矩阵乘法
- 构造函数,实例,原型,以及原型链
- RabbitMQ系列-顺序消费模式和迅速消息发送模式
- message show_once这个标志位的用法
- hadoop启动异常:UnregisteredDatanodeException
- cpu工作原理flash动画_17张PLC工作原理动画,每一个都是经典
- Python | 常见的反爬及解决方法,值得收藏
- ctf 改变图片高度_在Unity中 改变地形(Terrain),并加上水面、树、草地、材质(地板上色)...
- ABAQUS中获取参考点时间-位移曲线
- 深度学习实现视频分类的6种方法
- html 图片 透明颜色,CSS实现图片变灰色及透明度
- c语言循环中怎样读取多行,c++循环读取多行文本文件
- Java成长第五集--面向对象设计的五大原则
- OpenCV图像处理---模糊原理
- tensorflow中的shape函数理解
- 中国定制家具市场营销态势与盈利前景预测报告(2022-2028年)
- 关于H3C光模块和华为光模块的型号大全
- 离散傅里叶变换DFT、离散余弦变换DCT、离散正弦变换DST,原理与公式推导
- Http详解,2021年是做Android开发人员的绝佳时机
- AtCoder Beginner Contest 249题解(E,F)