NEW

The wait is over. SurrealDB 3.0 is here!

Learn more
Course content preview

5: Parentheses and accessing fields

The first item in an array is at index 0, so you can use [0] to grab it.

["first", "second", "third"][0];

Response

'first'

And since statements like CREATE return an array of results, you might want to pull out the result at a certain index too.

But if you just add [0] at the end of a CREATE statement it won't work, because place[0] is telling the database to take the first item of a value that doesn't exist yet (a table name place that it hasn't looked up yet), not the CREATE statement itself.

CREATE place[0];

Response

"Can not execute CREATE statement using value 'NONE'"

To fix it, surround the whole statement in parentheses first.

(CREATE place)[0];

Records have an object-like structure that holds keys (fields) and values. You can access a single field by using a dot and then the field name.

{
name: "Surreal library",
id: place:surreal_library
}.id;

Response

place:surreal_library

As above, you can surround a statement in parentheses and then choose a field to return.

Accessing a single field

(CREATE place).id;

The output is an array of IDs, because .id tells the database to go through the array and only gather the values of the id field.

Response

[
place:exqlj50tybc6yb1wjrx3
]

If you want to access more than one field, you can give the output a structure by adding a {} after the dot and putting the field names inside there.

Accessing multiple fields

(CREATE place SET place_type = "library", num_books = 10000).{
place_type,
num_books
};

Response

[
{
num_books: 10000,
place_type: 'library'
}
]