1
votes

I am creating Nodes City with properties Name and ID.City node may be indexed on Id.I want unique node.

Now when populating database I want to get City node if present otherwise I want to create a City node.

What is the best way to do that?

Create a Cypher execution engine and a unique constraint. And then Use MERGE to create a unique node.

Or something like

Check if the City with that id is present **Match 
START n=node:City(id = { id }) RETURN n**

if n==null Create the Node

Which is fast?What if I won't index city?

2

2 Answers

0
votes

Cypher is safe as it takes the correct locks.

The Java version would probably be a tiny bit faster, but you have to create the write locks on a single root-lock-entity yourself to make sure that in no scenario more than one thread accesses that code / data.

Without an index you have to scan the whole database to check for duplicates.

0
votes

use the info in http://docs.neo4j.org/chunked/stable/query-merge.html

MERGE (aCity:CITY {Id: '1', Name: 'aName') return aCity;

This would only create the node if it does not exist. If the node already exists it returns the existing node.

NOTE - if the name is spelled differently, a new node will be created even if the Id is the same as the existing node (a complete match of all fields is performed). If this is not the functionality you want you need to use the ON MATCH functionality described in the above link and put a unique constraint on the Id