Search code examples
neo4jcypherneo4j-apoc

Run Cypher query exactly N times


Is there a way to repeat a Cypher query exactly N times? Either in the web interface or console.

There are APOC procedures to run a query until it returns zero. But I did not find anything to run a query multiple times.


Solution

  • You can do it using the APOC procedure apoc.periodic.iterate:

    The docs about apoc.periodic.iterate says:

    With apoc.periodic.iterate you provide 2 statements, the first outer statement is providing a stream of values to be processed. The second, inner statement processes one element at a time (...)

    In the below example, the first statement is returning 10 elements. This way, the second statement will be executed 10 times, producing 10 :Person nodes.

    CALL apoc.periodic.iterate(
        "WITH RANGE(0,9) AS list UNWIND list as element RETURN element",
        "CREATE(:Person)", {}
    )