SurrealDB
SurrealDB Docs Logo

Enter a search query

Enter a search query

Available since: v3.0.0-alpha.12

Set functions

These functions can be used when working with, and manipulating sets of data.

FunctionDescription
set::add()Adds an item to a set if it does not exist
set::complement()Returns the complement of two sets
set::contains()Checks to see if a value is present in a set
set::difference()Returns the difference between two sets
set::intersect()Returns the values which intersect two sets
set::is_empty()Checks if a set is empty
set::len()Returns the length of a set
set::remove()Removes an item at a specific position from a set
set::union()Returns the unique merged values from two sets

set::add

The set::add function adds an item to a set.

API DEFINITION
set::add(set, value) -> set

If the item is not present in the set, it will be added.

{"one", "two"}.add("three"); -- {'one', 'three', 'two'}

Otherwise, the original set will be returned.

{"one", "two"}.add("two"); -- {'one', 'two'}

set::complement

The set::complement function returns the complement of two sets, namely a single set containing items which are not in the second set.

API DEFINITION
set::complement(set, set) -> set
Example with output
{1, 2, 3, 4}.complement({3, 4, 5, 6}); -- {1, 2}

set::contains

The set::contains function checks to see if a value is contained within a set.

API DEFINITION
set::concat(set, set) -> set
Example with output
{1, 2, 3}.contains(3); -- true

set::difference

The set::difference function determines the difference between two sets, returning a single set containing items which are not in both sets.

API DEFINITION
set::difference(set, set) -> set
Example with output
{1, 2, 3, 4}.difference({3, 4, 5, 6}); -- {1, 2, 5, 6}

set::intersect

The set::intersect function calculates the values which intersect two sets, returning a single set containing the values which are in both sets.

API DEFINITION
set::intersect(set, set) -> set
Example with output
{1, 2, 3, 4}.intersect({3, 4, 5, 6}); -- {3, 4}

set::is_empty

The set::is_empty function checks whether the set is empty or not.

API DEFINITION
set::is_empty(set) -> bool
Example with output
{1, 2, 3, 4}.is_empty(); -- false

set::len

The set::len function calculates the length of a set, returning a number. This function includes all items when counting the number of items in the set. If you want to only count truthy values, then use the count() function.

API DEFINITION
set::len(set) -> number
Example with output
{1, 2, 1, null, "something", 3, 3, 4, 0}.len(); -- 7

set::remove

The set::remove function removes an item of a certain value from a set.

API DEFINITION
set::remove(set, value) -> set

The following example removes the value 2 from a set.

{1, 2, 2, 2, 5}.remove(2); -- {1, 5}

If the value does not exist, the untouched set will be returned.

Removing
{1, 2, 2, 2, 5}.remove(3); -- {1, 2, 5}

set::union

The set::union function combines two sets together, removing duplicate values, and returning a single set.

API DEFINITION
set::union(set, set) -> set
Example with output
{1, 2, 1, 6}.union({1, 3, 4, 5, 6}); -- {1, 2, 3, 4, 5, 6}
Edit this page on GitHub