
9: Casting and aliases
SurrealDB has a lot of data types, with names in lowercase such as bool, number, string, and array. Most types can be quickly changed into another by using a casting function. To cast into another type, put the target type inside <> angle brackets to the left.
One of the fields is called "<string> floors", because by default a field with a value set by an operation will have the operation as its name.
[
{
"<string> floors": '8',
address: '2025 Statement Street, Riverdale',
name: 'Surreal Library'
}
]If you find this field name ugly or want to rename it for another reason, you can use AS to give it a different name (give it an alias).
[
{
address: '2025 Statement Street, Riverdale',
floors: '8',
name: 'Surreal Library'
}
]Casting will only work as long as the input is valid, mind you.
-------- Query 1 --------
d'1859-11-26T00:00:00Z'
-------- Query 2 --------
"Expected a datetime but cannot convert 'EIGHTEEN FIFTY NINE, NOVEMBER TWENTY SIX' into a datetime"
-------- Query 3 --------
"Expected a bool but cannot convert 'Hello there!' into a bool"Casting also won't work for complex types because the database can't tell what you want to do. Take this object for example that you would like to turn into an array.
-- This cast won't work
<array>{
address: '2025 Statement Street, Riverdale',
floors: '8',
name: 'Surreal Library'
};You might want an array holding each key, or arrays holding each key and value, and so on. In this case, take a look at SurrealDB's object functions to choose how to transform from one type into another. For example, you could use the object::values() function to just return the object's values.
[
'2025 Statement Street, Riverdale',
'8',
'Surreal Library'
]Or you could use object::from_entries() to do the opposite: constructing an object from arrays of arrays.
{
address: '2025 Statement Street, Riverdale',
floors: '8',
name: 'Surreal Library'
}The array::map() function is another great way to turn one type into another. After typing .map(), choose a parameter name inside || for each item, and then choose what to do with each item before passing it on.
This example of the function takes two strings and turns each one of them into an object with three fields. The output is an array of objects created by each string.
[
{
is_cool: true,
name: 'Surreal Library',
name_is_long: true
},
{
is_cool: true,
name: 'Riverdale',
name_is_long: false
}
]THE PLATFORM
Everything an application and its agents know. Five surfaces, one engine.
Database
Document, graph, vector, time-series and relational in one engine.

Agent Memory
What an agent learns, with its source and its time, in the same engine.

Cloud
Managed clusters in the regions you choose, scaled on demand.

Studio
Query, explore and design the schema from the browser.

MCP
Every model that speaks MCP reaches the database and the memory directly.

IN PRODUCTION
Trusted at scale. Samsung, Nvidia, Verizon, Tencent and Walmart run on SurrealDB.
14,000+
Developers building on SurrealDB Cloud
4M+
Developers building on SurrealDB worldwide
FROM THE TEAMS
SurrealDB gives us a foundation where we can unify semantic search, knowledge graphs, and AI-driven decision making without stitching together multiple systems. Collapsing responsibility into SurrealDB has become our default engineering posture.
VP of Engineering, Later