Skip to main content

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
}
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