0
votes

I'd like to bundle a whole bunch of operations in as few requests as possible. The logic behind is should be the following:

  • check if head with key=value exists, create if it doesn't
  • check if tail with key=value exists, create if it doesn't
  • create a unique relationship (all kinds of properties)
  • set a bunch of properties for head & tail

I already stumbled upon create unique, but this seems to work for relationships only. How do I make sure that at least the head node exist so I can run a create unique statement on it?

What I'm doing now is firing multiple requests.The first one to find out if the node exists:

start x=node:index({key}={value}) return ID(x) as id

If that doesn't return an id, I fire another request to create the node and finally the final request to create the second node and relationships:

start n=node({id})
create unique n-[:POINTS_TO {label:{label}}]->(x {{key}:{value}})
return n,x

I'm wondering if there's a nicer way to bundle of all this...

1

1 Answers

4
votes

if you use the auto-index, you can do:

start n=node:node_auto_index(key={value})
with count(*) as exists
where exists=0
create (n {key: {value}}
return n;

statement 2:

start n=node:node_auto_index(key={value})
create unique n-[:REL {foo:"bar"}]->(m {a:"b"})
set r.answer = 42
set m.c="d"
return r,m;

in Neo4j 2.0 there is MERGE for what you want.