Updating an expression updateexpression

Source: Internet
Author: User
Tags dynamodb

Theme:

    1. SET-Modify or Add project properties
    2. Remove-Removes a property from the project
    3. ADD-Update numbers and collections
    4. Delete-Removes an element from the collection

    • Set modify Properties

    

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"}, Expre Ssionattributevalues: {":p": 100.99, ": N": "100-100-010", ": C": 1},Returnvalues: "Updated_new"}; Await docclient.update (params). Promise (). Then ((success) = {Response.body = JSO                    N.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 };

The program ran successfully returned as follows:

{    "success:": {"        Attributes": {            "Count": 9,            "Price": 100.99,            "sn": "100-100-010"    }}}
    • adding lists and mappings

  

case ' POST ': {var table = "Book_table", hash = "book"; var params = {Tablename:table, Ite              M: {"Hash": hash, "id": UUID (), "BookType": [' Youth literature ', ' historical allusions ']            }};  Await Docclient.put (params). Promise (data) = {Response.body = Json.str  Ingify ({"Success": Data}). catch ((err) =>{response =                            {StatusCode:err.statusCode, body:JSON.stringify ({                Code:err.code, message:err.message})}            }) callback (null, response);        Break }

  


  

When the program runs successfully, it returns:

{    "Success": {}}

  

    • To add an element to the list:
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": ["Reader Magazine", "most novel", "Technology Imagination"]},}; Await Docclient.put (params). Promise (data) = {Response.body = Json.str  Ingify ({"Success": Data}). catch ((err) =>{response =                            {StatusCode:err.statusCode, body:JSON.stringify ({                Code:err.code, message:err.message})}            }) callback (null, response);        Break }

  

When the program runs successfully, it returns:

{"Success:": {"Attributes": {"            booktype": ["                Youth Literature",                "historical allusions",                "Reader's Magazine",                "most novel",                " Technology Imagination ",",            "hash": "book",            "id": "556433cf-37b6-4e12-b513-9c261fd279f7"}}    }
    • Adding nested mapping properties
Case ' POST ': {var table = "Book_table" and hash = "book";                    var params = {tablename:table, Item: {"Hash": hash, "id": UUID (),"type": {"List1": ["Martial Arts Fiction", "Social Science book"], "list2": ["Children's books "," comic book "]," list3 ": [" Law "," Economy "]}}              }; Await Docclient.put (params). Promise (data) = {Response.body = Json.str  Ingify ({"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": ["Military", "art "],": B3 ": [" Society "," Science "]}, Returnvalues:" All_new "}; Await docclient.update (params). Promise (). Then ((success) = {Response.body = JSO                    N.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 };

When the program runs successfully, it returns:

{"Success:": {"Attributes": {"            Type": {                "List1": [                    "Military",                    "Art"                ],                "List3": [                    " Society ",                    " Science "                ],                " List2 ": [                    " Children's books ",                    " comic books "                ]            },            " hash ":" book ",            " id ":" bee1bea0-4b6a-4fbc-8cf0-8c030519c909 "        }}    }
    • Adds and subtract numeric attributes (performs a decrement operation on existing numeric properties). To do this, use the + (plus) and - (minus) operators. )
Case ' POST ': {var table = "Book_table" and hash = "book"; var params = {tablename:table, Item: {"Hash": hash, "id": UUID (), "name": "Triple Gate", "Author": "Han Han", "type": "Sentiment                     Sense other "," SN ":" 100-100-008 "," Price ": 103.50," date ":" 2010-01-01 ", "Count": 7, "description": "This book, through the perspective of juvenile Lin Yuxiang, reveals to the reader the real life of high school students, reflecting student-style thinking, confusion and dreams." "                                }}; Console.log (params.            Item.count); Await Docclient.put (params). Promise (data) = {Response.body = Json.str  Ingify ({"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_ne W "}; Await docclient.update (params). Promise (). Then ((success) = {Response.body = JSO                    N.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 };

When the program runs successfully, it returns:

{"    success:": {"        Attributes": {            "date": "2010-01-01", "            Author": "Han Han",            "price": 99.99,            " Count ": 8,            " name ":" Triple Gate ",            " description ":" This book reveals the life of the real high school students through the juvenile Lin Yuxiang, reflecting the student-style thinking, perplexity and dream. " ",            " id ":" 4b99e00f-9f57-41f7-80de-d581e8c84522 ",            " sn ":" 100-100-100 ",            " type ":" Emotion Other ",            " hash ":" Book "}}    }
    • Attaching an element to a list
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 "}, E Xpressionattributevalues: {": T": ["Through Time and Space", "fantasy novel"]}, Returnvalues: "All _new "}; Await docclient.update (params). Promise (). Then ((success) = {Response.body = JSO                    N.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 };

When the program runs successfully, it returns:

{"Success:": {"Attributes": {"            booktype": ["                Youth Literature",                "historical allusions",                "Reader's Magazine",                "most novel",                " Technology Imagination ",                 " Through Time and Space ",                " fantasy novel "            ],            " hash ":" book ",            " id ":" 556433CF-37B6-4E12-B513-9C261FD279F7 "        }    }}
    • Prevents overwriting an existing property (sets the price of the item, but only if the item does not yet have the price property set. (If price already exists, no action is taken.) ))
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": Returnvalues: "All_new"}; Await docclient.update (params). Promise (). Then ((success) = {Response.body = JSO                    N.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 };

When the program runs successfully, it returns:

{"Success:": {"Attributes": {"            booktype": ["                Youth Literature",                "historical allusions",                "Reader's Magazine",                "most novel",                " Technology Imagination ",                " Through Time and Space ",                " fantasy novel "            ],            " id ":" 556433cf-37b6-4e12-b513-9c261fd279f7 ",            " price ": 100, "            Hash": "Book"}}    
    • Remove: Removes the property from the project (no action is taken if the property does not exist). )
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 = JSO                    N.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 };

When the program runs successfully, it returns:

{"Success:": {"Attributes": {"            booktype": ["                Youth Literature",                "historical allusions",                "Reader's Magazine",                "most novel",                " Technology Imagination ",                " Through Time and Space ",            ", "id": "556433cf-37b6-4e12-b513-9c261fd279f7", "Price": "," "            Hash": " Book "}}    }

Note: all_new will cause Dynamodb to return the item as it is updated, and Updated_new will only return the item as part of the update.

This article is original, still hope to teach!

If in doubt, you can leave a message below, or read the official AWS document--dynamodb:https://docs.aws.amazon.com/zh_cn/amazondynamodb/latest/developerguide/ Expressions.UpdateExpressions.html

Updating an expression updateexpression

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.