ADD_ELEMENT function

The ADD_ELEMENT row function adds an element to a set.

ADD_ELEMENT takes an atomic value and a set and returns that set with the atomic value added to it. The atomic value must be of the same data type as the current elements in the set. The atomic value is not added to the set if a duplicate value is already in the set. Note that the atomic value is not added to the set in the Dgraph, but only to the new, temporary set that is created by the ADD_ELEMENT function.

The syntax of the ADD_ELEMENT function is:
ADD_ELEMENT(<atomic-value>, <set>)
where:
  • atomic-value is an atomic value, such as 50 for an integer set or 'fifty' for a string set. It can also be a single-assign attribute. atomic-value will be added to set. The type of the atomic value must match the type of the set's elements.
  • set is a set to which atomic-value will be added. The elements of set must have the same set data type as atomic-value. For example, if atomic-value is a single-assign double attribute, then the elements of set must also be strings.
Examples of some results are as follows ({ } indicates an empty set):
ADD_ELEMENT(1, { 2, 3 }) = { 1, 2, 3 }
ADD_ELEMENT(1, { 1, 2 }) = { 1, 2 }
ADD_ELEMENT(NULL, { 1, 2 }) = { 1, 2 }
ADD_ELEMENT(1, { 'a', 'b' }) yields a checking error because the atomic value and the set elements are not of the same data type

ADD_ELEMENT example

In this example, the number 100 is added to the Score integer set (which currently does not have a value of 100 in it):
RETURN results AS
SELECT 
   WineID AS idRec,
   ADD_ELEMENT(100, Score) AS addAttrs
FROM WineState
WHERE WineID BETWEEN 10 AND 14
ORDER BY idRec
The result of this statement might be:
addAttrs              idRec
----------------------------
| { 100, 83, 85, 86 } | 10 |
| { 100, 82, 83 }     | 11 |
| { 100, 81, 89 }     | 12 |
| { 100, 73, 75 }     | 13 |
| { 100, 72, 74, 75 } | 14 |
----------------------------

The results show that the number 100 was added to the sets. For example, the Score set of Record 12 previously had 81 and 89 as its elements, but now has 81, 89, and 100 as the element values.