Update Expression
Overview
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
Syntax
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 (ACTION
).
(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 Expression
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[istart..iend]
ATTRIBUTE[i]
) — and the value to assign (VALUE
).
VALUE
can also be an expression that evaluates to the value to assign — for example, "SET myattr = 4 + 5;"
REMOVE Expression
REMOVE ATTRIBUTE
A REMOVE expression removes an attribute from an item (i.e., deletes the attribute).
The expression begins with the ATTRIBUTE
).
Examples
-
Increment the current value of an existing
miles attribute by 1000:"SET miles = miles + 1000"
-
Remove an item's
miles attribute:"REMOVE miles"
-
Update the values of two grocery-department income attributes (
produce anddairy ), and set the value of asum attribute to the sum of the updated department-income attribute values:"produce=20000; dairy=15000; sum = produce + dairy"
-
Set the value of a
rainbow string attribute:"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
ctr counter attribute to 1 if it doesn't already exist; if the attribute exists, increment its value by 1 (seeif_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
if_not_exists ):"SET color = if_not_exists(color, 'red'); SET updated = if_not_exists(updated, true)"
-
Switch the values of two attributes (
a andb ) using a temporarytemp attribute, and then delete the temporary attribute. Attributesa andb must already be defined for the item:"temp=b; b=a; a=temp; REMOVE temp"
-
Create a new
counters integer array attribute with five elements, all initialized by default to zero:"SET counters = init_array(5, 'int')"
-
Create a new
smallArray array attribute from the first five elements of an existingmyArray array attribute:"smallArray = myArray[0..4]"
-
Update the values of the first four elements of an
arr array 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[2]=-1; arr[0]=6; arr[1]=7; arr[3]=arr[0]+arr[2]*arr[1]"
-
Define an
arrSmallFibonacci integer 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_array default):"arrSmallFibonacci = init_array(5, 'int'); arrSmallFibonacci[1]=1; arrSmallFibonacci[2] = arrSmallFibonacci[0] + arrSmallFibonacci[1]; arrSmallFibonacci[3] = arrSmallFibonacci[1] + arrSmallFibonacci[2]; arrSmallFibonacci[4] = arrSmallFibonacci[2] + arrSmallFibonacci[3];"
-
Define an
arrFibonacci integer array attribute of 100 elements and ani loop-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
i and of the first three elements of thearrFibonacci array; (the initialization to 0 of the first element, at index 0, could have been omitted because it's the defaultvalue): "i=2; arrFibonacci = init_array(100,'int'); arrFibonacci[0]=0; arrFibonacci[1]=1; arrFibonacci[2] = arrFibonacci[0] + arrFibonacci[1]"
-
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:
"i<55"
-
At the end, you can optionally use a REMOVE update expression to delete the
i attribute:"REMOVE i"
-