An update expression is an expression that is used to update the attributes of an item.
This includes adding attributes and initializing their values, changing the value of existing attributes, or removing (deleting) item attributes.
For example, the NoSQL Web API
KEYWORD ACTION[; KEYWORD ACTION; ...]
An update expression is composed of one or more update-action expressions, each made up of an action keyword (
KEYWORD) followed by a matching action expression (
(In some cases, the keyword can be omitted, as outlined in the documentation.)
The action expressions are separated by semicolons (
Spaces around the semicolons are ignored.
The last update-action expression can optionally be terminated with a semicolon as well.
The following update expression types are supported:
- SET expression — sets the value of an item attribute (including creation of the attribute if it doesn't already exist) or of an element in an array attribute.
- REMOVE expression — removes (deletes) an item attribute.
SET ELEMENT = VALUE
A SET expression updates the value of an attribute or an element in an array attribute. When updating an attribute that isn't already found in the updated item, the platform creates the attribute and initializes it to the specified value.
A SET expression can optionally begin with the
=) whose operands are the updated element (
ELEMENT) — an attribute (
ATTRIBUTE), a slice of an array attribute (
ATTRIBUTE[i]) — and the value to assign (
VALUE can also be an expression that evaluates to the value to assign — for example,
"SET myattr = 4 + 5;"
A REMOVE expression removes an attribute from an item (i.e., deletes the attribute).
The expression begins with the
Increment the current value of an existing
milesattribute by 1000:
"SET miles = miles + 1000"
Remove an item's
Update the values of two grocery-department income attributes (
produceand dairy), and set the value of a sumattribute to the sum of the updated department-income attribute values:
"produce=20000; dairy=15000; sum = produce + dairy"
Set the value of a
"SET rainbow='red, orange, yellow, green, blue, indigo, violet'"
Initialize multiple attributes of different types in a person item:
"SET name='Alexander Bell'; country='Scotland'; isMarried=true; numChildren=4"
Add and initialize a
ctrcounter attribute to 1 if it doesn't already exist; if the attribute exists, increment its value by 1 (see if_not_exists):
"SET ctr = if_not_exists(ctr,0) + 1;"
Add new attributes to an item and initialize their values; if the attributes already exist, they will be reassigned their current values (see
"SET color = if_not_exists(color, 'red'); SET updated = if_not_exists(updated, true)"
Switch the values of two attributes (
aand b) using a temporary tempattribute, and then delete the temporary attribute. Attributes aand bmust already be defined for the item:
"temp=b; b=a; a=temp; REMOVE temp"
Create a new
countersinteger array attribute with five elements, all initialized by default to zero:
"SET counters = init_array(5, 'int')"
Create a new
smallArrayarray attribute from the first five elements of an existing myArrayarray attribute:
"smallArray = myArray[0..4]"
Update the values of the first four elements of an
arrarray attribute. The fourth attribute (at index 3) is assigned the result of an arithmetic expression that uses the updated values of the first three array elements:
"arr=-1; arr=6; arr=7; arr=arr+arr*arr"
arrSmallFibonacciinteger array attribute of five elements that implements a small Fibonacci series in which each element (beginning with the third element) is the sum of the previous two elements; (the value of the first element, at index 0, is 0 — init_arraydefault):
"arrSmallFibonacci = init_array(5, 'int'); arrSmallFibonacci=1; arrSmallFibonacci = arrSmallFibonacci + arrSmallFibonacci; arrSmallFibonacci = arrSmallFibonacci + arrSmallFibonacci; arrSmallFibonacci = arrSmallFibonacci + arrSmallFibonacci;"
arrFibonacciinteger array attribute of 100 elements and an iloop-iterator number attribute, and use the attributes to implement a Fibonacci series in which each element (beginning with the third element) is the sum of the previous two elements.
Define the attributes and initialize the values of
iand of the first three elements of the arrFibonacciarray; (the initialization to 0 of the first element, at index 0, could have been omitted because it's the default value):
"i=2; arrFibonacci = init_array(100,'int'); arrFibonacci=0; arrFibonacci=1; arrFibonacci = arrFibonacci + arrFibonacci"
Repeatedly update the item using the following update expression to sequentially update the series elements in the array (starting from the fourth element at index 3):
"i=i+1; arrFibonacci[i] = arrFibonacci[i-1] + arrFibonacci[i-2];"
You can use a condition expression to set the maximum number of array elements to update. For example, the following expression limits the update to 55 elements:
At the end, you can optionally use a REMOVE update expression to delete the