Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

FOR Loops

⚠️ Warning

HelixQL is deprecated in HelixDB v2. Queries are now written with the Rust DSL and dispatched as JSON — see the Querying guide. This section is kept as a reference for legacy HelixQL projects.

For the complete documentation index optimized for AI agents, see llms.txt.

Overview

FOR loops enable you to iterate over collections and perform operations on each element. They are essential for batch processing, data transformation, and building complex graph structures.

FOR variable IN collection {
    // Operations on each element
}

FOR {field1, field2} IN collection {
    // Operations with destructured fields
}

ℹ️ Note

FOR loops are executed sequentially, making them ideal for operations that need to maintain order or have dependencies between iterations.

When to use FOR loops

  • Batch data loading: Insert multiple nodes or edges from a collection
  • Data transformation: Process and update multiple records
  • Graph construction: Build relationships between collections of nodes
  • Nested iterations: Create connections between all pairs of elements

Quick examples

Basic iteration

FOR user IN users {
    AddN<User>({ name: user.name, age: user.age })
}

Destructuring

FOR {name, age, email} IN user_data {
    AddN<User>({ name: name, age: age, email: email })
}

Nested loops

FOR user1 IN users {
    FOR user2 IN users {
        AddE<Knows>::From(user1)::To(user2)
    }
}

Detailed guides