
18: FOR loops
Another way to work with the items inside an array is to use a FOR loop. After FOR, choose a parameter name (like $item or $person) to be used every time the loop occurs, follow it with IN and the data, and then {} to open up a space in which you can write custom logic for each item in the loop.
Inside the loop in the next query, the parameter $person can be used to set the fields of each person record. Since each step of a FOR loop is isolated inside its own space, the output of a FOR loop will always be empty. To see the results of the person records created in this way, we will need to use a SELECT statement after the loop is done.
[
{
age: 45,
id: person:cj8wc617o1ea2vs1yv4n,
name: 'Samm Schwartz'
},
{
age: 30,
id: person:e8z6pjqhuwzfalbku61n,
name: 'Sara Bellum'
},
{
age: 25,
id: person:faovorn8ntkqwfw01638,
name: 'Lydia Wyndham'
}
]This is the way that we will create these employees for the library, but we also want to link each one to the library at each step of the FOR loop after creating them (so run a quick DELETE person at this point if you are following along separately). This time the link will be something called a graph edge, which we will first need to learn to use starting on the next page.
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