SurrealDB Docs Logo

Enter a search query

Sets

A set is similar to an array, but deduplicates items.

DEFINE FIELD visited ON TABLE traveler TYPE set<record<country>>; CREATE traveler:one SET visited = [country:canada, country:usa, country:korea, country:japan]; -- Traveler comes back from a trip UPDATE traveler:one SET visited += country:uk; -- And again UPDATE traveler:one SET visited += country:uk;
Response
[ { id: traveler:one, visited: [ country:canada, country:usa, country:korea, country:japan, country:uk ] } ]

Internally, a set is identical to an array – even inside a schema definition. The only difference is that a field defined as a set will never hold a duplicate item. Otherwise, a field defined as an array can take a set as input and vice versa.

DEFINE FIELD bank_accounts ON TABLE customer TYPE set<int>; DEFINE FIELD languages ON TABLE customer TYPE array<string>; CREATE customer SET bank_accounts = [ 55555, 55555, 98787 ], languages = <set>[ "en", "ja", "kr", "kr" ];
Output
[ { bank_accounts: [ 55555, 98787 ], id: customer:uv6mn62t8td9vzvfogh4, languages: [ 'en', 'ja', 'kr' ] } ]

Casting into a set can be a convenient way to deduplicate items in the same way that the array::distinct() function is used.

LET $array = [1,1,3,4,4,4,4,4,4]; RETURN [ $array.distinct(), <set>$array ];
Output
[ [ 1, 3, 4 ], [ 1, 3, 4 ] ]

For all other behaviour and uses of a set, please see the page on arrays.

© SurrealDB GitHub Discord Community Cloud Features Releases Install