Graeme Newlands
Graeme Newlands

Reputation: 73

Constant values in Neo4j Cypher Query

I'd like to be able to tidy up some longer cypher queries that I'm using in the neo4j console.

In the spirit of the Pretty graphs page in the neo4j documentation, choosing an arbitrary example, say I have the query to create a complete six node graph, K_6:

foreach (x in range(0,5) | create (n:Node {val: x}))
with *
unwind range(0,5) as x
with x
unwind range(0,5) as y
match (n:Node), (m:Node) where n.val = x and m.val = y and n.val <> m.val create (n)-[:X]->(m)

Ideally I'd be able to take out the five in the range(0,5) and replace it with a constant expression like NODE_COUNT for range(0,NODE_COUNT).

Are comments the only way to improve the legibility of a query such as that above?

Upvotes: 1

Views: 1148

Answers (1)

Michael Hunger
Michael Hunger

Reputation: 41676

you can even take out the whole range,

I usually do that by prefixing the query with a WITH statement.

So either:

WITH 5 AS node_count
foreach (x in range(0,node_count) | create (n:Node {val: x}))
...

or even

WITH range(0,5) AS node_range
foreach (x in node_range | create (n:Node {val: x}))
...

Upvotes: 1

Related Questions