Booleans
Boolean values can be used to mark whether a field is true or false.
CREATE person SET newsletter = false, interested = true;
Many SurrealDB operators and functions return booleans.
CREATE person SET
name = "Billy",
name_is_billy = name = "Billy",
name_is_long = string::len(name) > 10;
Response
[
{
id: person:7j4t4higwb141v1v2xum,
name: 'Billy',
name_is_billy: true,
name_is_long: false
}
]
Boolean values can be written in anycase.
CREATE person SET
newsletter = FALSE,
interested = True,
very_interested = trUE;
Booleans in WHERE clauses
When performing a query on the database, accessing a record’s ID directly or using a record range allows performance to be significantly sped up by avoiding the table scan which is used when a WHERE clause is included.
However, if a WHERE clause is unavoidable, performance can still be improved by simplifying the portion after the clause as much as possible. As a boolean is the simplest possible datatype, having a boolean field that can be used in a WHERE clause can significantly improve performance compared to a more complex operation.
DEFINE FIELD data_length ON person VALUE random_data.len();
DEFINE FIELD is_short ON person VALUE random_data.len() < 10;
CREATE |person:10000| SET random_data = rand::string(1000) RETURN NONE;
CREATE person:one SET random_data = "HI!" RETURN NONE;
SELECT * FROM person WHERE random_data.len() < 10;
SELECT * FROM person WHERE data_length < 10;
SELECT * FROM person WHERE is_short;
SELECT * FROM person:one;