DynamoDB-删除表达式
语法:
- [Remove action[,action]...]——从项目中删除属性
- [DELETE action[,action]...]——从集合中删除元素/项目
- 删除项目
您可以使用 delete
方法,通过指定项目的主键来删除一个项目。您可以提供一个 ConditionExpression
以防止在不符合条件时删除项目。
var AWS = require('aws-sdk'); var fs = require('fs'); var uuid = require('uuid'); AWS.config.update({ region: "us-west-2", endpoint: "http://dynamodb:8000" }) var docClient = new AWS.DynamoDB.DocumentClient(); exports.handler = async function( event, context, callback ){ response = { headers: { "Content-Type": 'application/json', "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Credentials": true, "Access-Control-Allow-Headers": "Content-Type, Access-Control-Allow-Headers, Access-Control-Allow-Origin, Authorization, X-Requested-With" }, } case 'DELETE': { var table ='book_table', hash = 'book'; id = 'bee1bea0-4b6a-4fbc-8cf0-8c030519c909'; var params = { TableName: table, Key: { "hash": hash, "id": id }, ConditionExpression: "price < :h", ExpressionAttributeValues: {":h" : 50}, 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; }}
- 添加集合
1 var AWS = require('aws-sdk'); 2 var fs = require('fs'); 3 var uuid = require('uuid'); 4 5 AWS.config.update({ 6 region: "us-west-2", 7 endpoint: "http://dynamodb:8000" 8 }) 9 10 var docClient = new AWS.DynamoDB.DocumentClient(); 11 12 exports.handler = async function( event, context, callback ){ 13 14 response = { 15 headers: { 16 "Content-Type": 'application/json', 17 "Access-Control-Allow-Origin": "*", 18 "Access-Control-Allow-Credentials": true, 19 "Access-Control-Allow-Headers": "Content-Type, Access-Control-Allow-Headers, Access-Control-Allow-Origin, Authorization, X-Requested-With" 20 }, 21 } 22 23 case 'POST': { 24 var table = "book_table", hash = "test"; 25 26 var params = { 27 TableName: table, 28 Item: { 29 "hash": hash, 30 31 "id": uuid(), 32 33 "book_type": docClient.createSet(["青春文学", "历史典故", "杂志", "小说"]) 34 } 35 36 }; 37 await docClient.put( params ).promise().then( 38 ( data ) => { 39 response.body = JSON.stringify({"success": data}) 40 } 41 ).catch( 42 ( err ) =>{ 43 response = { 44 statusCode: err.statusCode, 45 body: JSON.stringify({ 46 code: err.code, 47 message: err.message 48 }) 49 } 50 } 51 ) 52 53 callback( null, response ); 54 break; 55 } 56 }
程序运行成功后返回:
1 { 2 "success:": { 3 "Attributes": { 4 "book_type": { 5 "wrapperName": "Set", 6 "values": [ 7 "历史典故", 8 "小说", 9 "青春文学", 10 “杂志” 11 ], 12 "type": "String" 13 }, 14 "hash": "test", 15 "id": "87de4a25-7361-4fcf-af01-5949c55099a3" 16 } 17 } 18 }
- 从集合中删除项目
1 var AWS = require('aws-sdk'); 2 var fs = require('fs'); 3 var uuid = require('uuid'); 4 5 AWS.config.update({ 6 region: "us-west-2", 7 endpoint: "http://dynamodb:8000" 8 }) 9 10 var docClient = new AWS.DynamoDB.DocumentClient(); 11 12 exports.handler = async function( event, context, callback ){ 13 14 response = { 15 headers: { 16 "Content-Type": 'application/json', 17 "Access-Control-Allow-Origin": "*", 18 "Access-Control-Allow-Credentials": true, 19 "Access-Control-Allow-Headers": "Content-Type, Access-Control-Allow-Headers, Access-Control-Allow-Origin, Authorization, X-Requested-With" 20 }, 21 } 22 case 'PUT': { 23 var table = 'book_table', hash = 'test'; 24 var id = "87de4a25-7361-4fcf-af01-5949c55099a3"; 25 var params = { 26 TableName: table, 27 Key: { 28 "hash": hash, 29 "id": id 30 }, 31 32 UpdateExpression: "DELETE book_type :value", 33 ExpressionAttributeValues: { 34 ":value": docClient.createSet(["杂志"]) 35 }, 36 ReturnValues: "ALL_NEW" 37 }; 38 39 await docClient.update( params ).promise().then( 40 ( success ) => { 41 response.body = JSON.stringify({ "success:": success }) 42 } 43 44 ).catch( 45 ( err ) => { 46 console.log(err); 47 response.statusCode = err.statusCode; 48 response.body = JSON.stringify({ 49 code: err.code, 50 message: err.message 51 }) 52 } 53 ); 54 callback( null, response ); 55 break; 56 };}
程序运行成功后返回:
-
-
1 { 2 "success:": { 3 "Attributes": { 4 "book_type": { 5 "wrapperName": "Set", 6 "values": [ 7 "历史典故", 8 "小说", 9 "青春文学" 10 ], 11 "type": "String" 12 }, 13 "hash": "test", 14 "id": "87de4a25-7361-4fcf-af01-5949c55099a3" 15 } 16 } 17 }
-
- 从项目中删除属性
1 var AWS = require('aws-sdk'); 2 var fs = require('fs'); 3 var uuid = require('uuid'); 4 5 AWS.config.update({ 6 region: "us-west-2", 7 endpoint: "http://dynamodb:8000" 8 }) 9 10 var docClient = new AWS.DynamoDB.DocumentClient(); 11 12 exports.handler = async function( event, context, callback ){ 13 14 response = { 15 headers: { 16 "Content-Type": 'application/json', 17 "Access-Control-Allow-Origin": "*", 18 "Access-Control-Allow-Credentials": true, 19 "Access-Control-Allow-Headers": "Content-Type, Access-Control-Allow-Headers, Access-Control-Allow-Origin, Authorization, X-Requested-With" 20 }, 21 } 22 case 'PUT': { 23 var table = 'book_table', hash = 'test'; 24 var id = "556433cf-37b6-4e12-b513-9c261fd279f7"; 25 var params = { 26 TableName: table, 27 Key: { 28 "hash": hash, 29 "id": id 30 }, 31 UpdateExpression: "REMOVE price", 32 33 34 ReturnValues: "ALL_NEW" 35 }; 36 37 await docClient.update( params ).promise().then( 38 ( success ) => { 39 response.body = JSON.stringify({ "success:": success }) 40 } 41 42 ).catch( 43 ( err ) => { 44 console.log(err); 45 response.statusCode = err.statusCode; 46 response.body = JSON.stringify({ 47 code: err.code, 48 message: err.message 49 }) 50 } 51 ); 52 callback( null, response ); 53 break; 54 }; 55 }
程序运行成功后返回:
1 { 2 "success:": { 3 "Attributes": { 4 "booktype": [ 5 "青春文学", 6 "历史典故", 7 "读者杂志", 8 "最小说", 9 "科技畅想", 10 "穿越时空" 11 ], 12 "hash": "book", 13 "id": "556433cf-37b6-4e12-b513-9c261fd279f7" 14 } 15 } 16 }
ps: 本文为本人原创文章,若有疑问,欢迎下方留言,也可自读aws的相关api文档: https://docs.aws.amazon.com/zh_cn/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html
用我拥有,换你没有,竭尽所有