Skip to main content
Version: 1.x

Arrays

Arrays in SurrealDB can any values, with no limit to their depth.

CREATE person SET results = [
{ score: 76, date: "2017-06-18T08:00:00Z", name: "Algorithmics" },
{ score: 83, date: "2018-03-21T08:00:00Z", name: "Concurrent Programming" },
{ score: 69, date: "2018-09-17T08:00:00Z", name: "Advanced Computer Science 101" },
{ score: 73, date: "2019-04-20T08:00:00Z", name: "Distributed Databases" },
];
CREATE location SET coordinates = [
[
[
[40.0, 40.0],
[20.0, 45.0],
[45.0, 30.0],
[40.0, 40.0]
]
],
[
[
[20.0, 35.0],
[10.0, 30.0],
[10.0, 10.0],
[30.0, 5.0],
[45.0, 20.0],
[20.0, 35.0]
],
[
[30.0, 20.0],
[20.0, 15.0],
[20.0, 25.0],
[30.0, 20.0]
]
]
];

Array sorting

Arrays can be sorted and compared. This is done one index at a time, starting with the first.

Returns true because 1 is greater than 0:

RETURN [1, 0, 0, 0, 0] > [0, 100, 100, 100, 100];

Returns false because 1 is equal to 1, after which the next index is compared: 0 which is less than 100.

RETURN [1, 0, 0, 0, 0] > [1, 100, 100, 100, 100];

As arrays can be used as record IDs, this sorting behaviour allows you to customize sorting behaviour in queries.

INSERT INTO measurement (id) VALUES
([ weather_station:1, '2024-05-15T04:00:24.420Z' ]),
([ weather_station:1, '2024-05-14T04:00:24.420Z' ]),
([ weather_station:1, '2024-05-13T04:00:24.420Z' ]),
([ weather_station:2, '2024-05-15T04:00:24.420Z' ]),
([ weather_station:2, '2024-05-14T04:00:24.420Z' ]),
([ weather_station:2, '2024-05-13T04:00:24.420Z' ]);

LET $start = time::now() - 5y;
LET $end = time::now() + 5y;
LET $ws = weather_station:1;

SELECT * FROM measurement:[ $ws, $start ]..=[ $ws, $end ];
Response
[
{
id: measurement:[
weather_station:1,
'2024-05-13T04:00:24.420Z'
]
},
{
id: measurement:[
weather_station:1,
'2024-05-14T04:00:24.420Z'
]
},
{
id: measurement:[
weather_station:1,
'2024-05-15T04:00:24.420Z'
]
}
]

For more on ID sorting, filtering and ranges, see the page on record IDs.