DynamoDB-条件表达式ConditionExpression
主题:
- 防止覆盖项目现有的项目
- 检查项目中的属性
- 有条件删除
- 有条件更新
- 比较运算符和函数引用
条件表达式的语法:
condition-expression ::=
operand
comparatoroperand
|operand
BETWEENoperand
ANDoperand
|operand
IN (operand
(','operand
(, ...) ))
| function
|condition
ANDcondition
|condition
ORcondition
| NOTcondition
| (condition
)comparator ::=
=
| <>
| <
| <=
| >
| >=function ::=
attribute_exists (path
)
| attribute_not_exists (path
)
| attribute_type (path
,type
)
| begins_with (path
,substr
)
| contains (path
,operand
)
| size (path
)
- 防止覆盖项目现有的项目( attribute_not_exists、
attribute_exists
)
PutItem 操作将覆盖具有相同键的项目 (如果存在)。如果要避免这种情况,请使用条件表达式。这仅允许写入在有问题的项目没有相同的键时继续进行:
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 price = :p",ConditionExpression: "attribute_not_exists(#c)",ExpressionAttributeNames: {"#c": "count"},ExpressionAttributeValues: {":p": 99.99}, 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": "9f3f9c74-8e23-4e46-b440-4b3532048ad6","price": 99.99,"hash": "book"}}
}
- 检查项目中的属性(attribute_not_exists、
attribute_exists
)
您可以检查任何属性是否存在。如果条件表达式的计算结果为 true,操作将成功;否则操作将失败。
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 price = :p",ConditionExpression: "attribute_exists(#c)",ExpressionAttributeNames: {"#c": "count"},ExpressionAttributeValues: {":p": 55.55},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": 55.55,"zan": 2048,"count": 10,"name": "三重门","description": "本书通过少年林雨翔的视角,向读者揭示了真实的高中生的生活,体现了学生式的思考、困惑和梦想。","id": "05d101be-d5e8-43ec-8eb6-5530e21af83e","sn": "100-100-010","type": "情感其他","hash": "book"}}
}
- 有条件删除
要执行有条件删除,请将 DeleteItem
操作与条件表达式一起使用。要继续执行操作,条件表达式的求值结果必须为 true;否则操作将失败。
使用 BETWEEN
和 IN
关键字来将操作数与值范围或值的枚举值列表进行比较:
- 如果a
BETWEENb
ANDc
a
大于或等于b
并且小于或等于c
,则为 true。
- 如果a
IN (b
,c
,d
)a
等于列表中的任何值(例如,b
、c
或d
任意之一),则为 true。列表最多可以包含 100 个值,以逗号分隔。
例如:
{"booktype": ["青春文学","历史典故"],"id": "9f3f9c74-8e23-4e46-b440-4b3532048ad6","price": 99.99,"hash": "book"
}
case 'DELETE': {var table ='book_table', hash = 'book', id = '9f3f9c74-8e23-4e46-b440-4b3532048ad6';var params = {TableName: table,Key: {"hash": hash,"id": id},
ConditionExpression: "(booktype[0] IN (:c1, :c2)) and (price between :p1 and :p2)",ExpressionAttributeValues: {":c1": "青春文学",":c2": "愿风裁尘",":p1": 90,":p2": 110},await docClient.delete( params ).promise().then(( data ) => {response.body = JSON.stringify({"success": data});console.log("success", data );}).catch(( err ) =>{console.log("error", err );response.statusCode = err.statusCode;response.body = JSON.stringify({"error": err});});callback( null, response );break;}
程序运行成功后返回:
{"success": {}
}
- 有条件更新
要执行有条件更新,请将 UpdateItem
操作与条件表达式一起使用。要继续执行操作,条件表达式的求值结果必须为 true;否则操作将失败。
注意:UpdateItem 还支持更新表达式,您在其中指定要对项目进行的修改和更改
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 price = price - :discount",ConditionExpression: "price > :limit",ExpressionAttributeValues: {":discount": 50,":limit": 1000},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": 974,"zan": 2048,"count": 10,"name": "三重门","description": "本书通过少年林雨翔的视角,向读者揭示了真实的高中生的生活,体现了学生式的思考、困惑和梦想。","id": "05d101be-d5e8-43ec-8eb6-5530e21af83e","sn": "100-100-010","type": "情感其他","hash": "book"}}
}
- 比较运算符和函数引用
- begins_with(path, substr): 如果
指定的属性以特定子字符串开头,则为 true。path
UpdateExpression: "SET zan = :z",ConditionExpression: "begins_with(#u, :v)",ExpressionAttributeNames: {"#u": "uri"},ExpressionAttributeValues: {":z": 1024,":v": "https://www.cnblogs.com"},ReturnValues: "ALL_NEW"
程序运行成功后返回:
- begins_with(path, substr): 如果
{"success:": {"Attributes": {"date": "2010-01-01","author": "韩寒","zan": 1024,"price": 55.55,"count": 10,"name": "三重门","description": "本书通过少年林雨翔的视角,向读者揭示了真实的高中生的生活,体现了学生式的思考、困惑和梦想。","id": "05d101be-d5e8-43ec-8eb6-5530e21af83e","sn": "100-100-010","type": "情感其他","uri": "https://www.cnblogs.com/landen/p/9790704.html ","hash": "book"}}
}
- attribute_type (
path
,type
): 如果指定路径中的属性为特定数据类型,则为 true。
1 UpdateExpression: "SET zan = :z", 2 ConditionExpression: "attribute_type(#p,:v_sub)", 3 ExpressionAttributeNames: { 4 "#p": "price" 5 }, 6 ExpressionAttributeValues: { 7 ":z": 520, 8 ":v_sub": "N" 9 }, 10 ReturnValues: "ALL_NEW"
程序运行成功后返回:
{"success:": {"Attributes": {"date": "2010-01-01","author": "韩寒","zan": 520,"price": 55.55,"count": 10,"name": "三重门","description": "本书通过少年林雨翔的视角,向读者揭示了真实的高中生的生活,体现了学生式的思考、困惑和梦想。","id": "05d101be-d5e8-43ec-8eb6-5530e21af83e","sn": "100-100-010","type": "情感其他","uri": "https://www.cnblogs.com/landen/p/9790704.html ","hash": "book"}} }
ps: 本文为本人原创文章,若有疑问,欢迎下方留言,也可自读aws的相关api文档: https://docs.aws.amazon.com/zh_cn/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html
转载于:https://www.cnblogs.com/landen/p/9790704.html
DynamoDB-条件表达式ConditionExpression相关推荐
- 2021年大数据常用语言Scala(七):基础语法学习 条件表达式
条件表达式 条件表达式就是if表达式,if表达式可以根据给定的条件是否满足,根据条件的结果(真或假)决定执行对应的操作.scala条件表达式的语法和Java一样. 有返回值的if 与Java不一样的是 ...
- 改善代码设计 —— 简化条件表达式(Simplifying Conditional Expressions)
系列博客 1. 改善代码设计 -- 优化函数的构成(Composing Methods) 2. 改善代码设计 -- 优化物件之间的特性(Moving Features Between Objects) ...
- JAVA条件表达式的陷阱
Map<String, Integer> map = new HashMap<String, Integer>(); map.put("count", n ...
- django 1.8 官方文档翻译:2-5-9 条件表达式
条件表达式 New in Django 1.8. 条件表达式允许你在过滤器.注解.聚合和更新操作中使用 if ... elif ... else的逻辑.条件表达式为表中的每一行计算一系列的条件,并且返 ...
- shell中的条件表达式
条件表达式返回的结果都为布尔型 真为1,假为0 条件测试的表达式 [expression] [[expression]] test expression 这三种条件表达式的效果是一样的 比较符 整数比 ...
- 【C语言探索之旅】 第一部分第六课:条件表达式
内容简介 1.课程大纲 2.第一部分第六课:条件表达式 3.第一部分第七课预告:循环语句 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C语言编写三个游戏. ...
- python 条件表达式换行_Python基础语法 - LongKing-Xu的个人空间 - OSCHINA - 中文开源技术交流社区...
python基础语法 一.标识符 在Python中,所有标识符可以包括英文.数字以及下划线(_),但不能以数字开头. 在Python中的标识符是区分大小写的. 在Python中以下划线开头的标识符是有 ...
- python条件表达式有哪几个_python条件表达式:多项分支,双向分支
# ### 多项分支 ''' if 条件表达式1: code1 code2 elif 条件表达式2: code3 code4 elif 条件表达式3: code5 code6 else: code7 ...
- 重构——39以多态取代条件表达式(Replace Conditional with Polymorphism)
以多态取代条件表达式(Replace Conditional with Polymorphism) 你手上有个条件表达式,它根据对象类型的不同而选择不同的行为:将这个条件表达式的每个分支放进一个子类内 ...
最新文章
- Digital Imaging Processing 数字图像处理
- Py之logging:logging的简介、安装、使用方法之详细攻略
- ARM64的启动过程之(一):内核第一个脚印
- 数据可视化 信息可视化_更好的数据可视化的8个技巧
- 第46课 精益求精 《小学生C++趣味编程》
- 1203. 项目管理
- linux汇编指令输出到屏幕,Linux 汇编语言(GNU GAS汇编)开发指南
- Mac 终端失效如何解救
- Android作业四
- Android 11 存储权限适配指南
- extjs 渲染之前的方法_extjs重新渲染组件
- Java版本企业招投标采购管理系统源码 一站式全流程采购招标系统
- 网页设计作业——小米商城官网首页(1页) HTML+CSS+JavaScript web期末作业设计网页_清新淡雅个人网页大学生网页设计作业成品
- VM虚拟机Ubuntu21.04 升级为22.04
- e1000网卡驱动第二天
- 【推荐】智慧检察公益诉讼辅助快检AI人工智能大数据平台解决方案合集(共183份,928M)
- 针对零基础的UE开发(05)
- SCT2450,SCT2450Q,降压DCDC
- am命令发送广播以及查看已发送广播信息
- 毕业生关于签约、毁约和存档的介绍---一个很好的科普
热门文章
- 《汇编语言程序设计》——仿windows计算器
- c语言性考任务答案,国家开放大学C语言程序设计A第二次形考任务及答案之欧阳音创编...
- 计算机集中控制系统结构上和DCS基本一致,集散控制系统与现场总线试题习题及答案全解.doc...
- 信号完整性(SI)电源完整性(PI)学习笔记(三)阻抗与电气模型
- SSM整合配置以及测试
- Python:对文件的操作
- python爬虫scrapy框架基础
- iOS开发的一些奇巧淫技3
- 阿里物联网套件-服务端SDK学习实践(基础篇-12推送数据给设备并得到响应)
- 华为平板能运行python吗_华为的这款机器不仅可以当笔记本电脑,还能当平板使用...