I am using CSV load into my neo4j and I'm struggling constructing a query for one of my usecases. I have a file with contents like:
aNodeFoo,xNodeBar
1,5
2,10
3,
4,
There are other properties of start node in the file, but this is irrelevant to the question. What I'd like to achieve is:
- create/merge starting node A and set its property foo to whatever is the value of the column
- if xNodeBar is populated, create a relationship to (existing) node X with property bar equal to given value, but if the cell is empty - simply ignore it.
In other words, for this example I expect to have nodes X with property bar set to 5 and 10 already, and I'd expect 4 nodes A with property foo set to 1,2,3 and 4 created, and node A foo=1 linked to node X bar=5 and node A foo=2 linked to node X bar=10.
If all lines are guaranteed to be populated then query is fairly simple:
LOAD CSV WITH HEADERS FROM ... AS csvLine
MATCH (nodeX:X {bar: csvLine.xNodeBar})
MERGE (nodeA:A {foo: csvLine.aNodeFoo})
MERGE (nodeA) -[:R]-> (nodeX)
I tried using OPTIONAL MATCH instead (which finds nothing for nodeX if cell is empty) but then I get an error saying I'm trying to link into a null node.
Hints anyone? Is such a query possible in cypher?