Martin Preusse
Martin Preusse

Reputation: 9369

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.

Upvotes: 1

Views: 553

Answers (2)

Aleksander Lech
Aleksander Lech

Reputation: 1325

In case you do not want to involve APOC currently you can just do as follows:

UNWIND range(0,500) as iterations
CREATE(:Person);

Upvotes: 1

Bruno Peres
Bruno Peres

Reputation: 16365

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)", {}
)

Upvotes: 4

Related Questions