I am using the nodejs package to run cypher queries against neo4j. I find the UNWIND option very handy to bulk-insert data.
Some of my nodes can have multiple relationships, but since I am doing a bulk insertion, some relationship IDs in the source data are null and then the cypher query fails for those objects.
Here is what I have done:
Some entries to UNWIND:
var entries = [
{
eid: '12313',
ident: 'ID1',
v: 123123,
ref1_eid: '1492082',
ref2_eid: '285654'
},
{
eid: '22325',
ident: 'ID2',
v: 551,
ref1_eid: null,
ref2_eid: '1490392825'
},
{
eid: '1163',
ident: 'ID3',
v: 9985,
ref1_eid: '12466',
ref2_eid: null
}
];
The cypher query looks like this:
UNWIND $entries as res
WITH res as res
MERGE (me:Me {eid: res.eid, ident: res.ident, v: res.v})
WITH me, res MATCH (a:TypeA {eid: res.ref1_eid})
MERGE (me)-[:GOES_TO]->(a)-[:GOES_FROM]->(me)
WITH me, res MATCH (b:TypeB {eid: res.ref2_eid})
MERGE (me)-[:GOES_TO]->(b)-[:GOES_FROM]->(me);
I run the command like this then
await session.run(
query, {entries: entries}
)
The first object is entered completely with the two relationships. The third one has the first relationship, but not the second, since that is null (which is expected). But the second object does not contain any relationships, since ref1_eid
is null
and the execution stops after the unsuccessful MATCH.
How could I achieve this bulk insert, where only those relationships are skipped, where nodes can't be found, but all others are added?