Skip to main content
Version: 2.x(alpha)

FOR statement

The FOR statement can be used to iterate over the values of an array, and to perform certain actions with those values.

SurrealQL Syntax
FOR @item IN @iterable @block

Example usage

The following query shows example usage of this statement.

-- Create a person for everyone in the array
FOR $name IN ['Tobie', 'Jaime'] {
CREATE type::thing('person', $name) CONTENT {
name: $name
};
};

The following query shows the FOR statement being used update a property on every user matching certain criteria.

-- Set can_vote to true for every person over 18 years old.
FOR $person IN (SELECT VALUE id FROM person WHERE age >= 18) {
UPDATE $person SET can_vote = true;
};