title: neo4j cypher update existing node or create new node
body: <p>I have a graph with approximately nine million nodes, and twelve million relationships. For each of the nodes within the graph there is a subset of properties for each respective node which forms a unique identity for the node, by Label. The graph is being updated by various data sources which augment existing nodes within the graph, or create new nodes if the nodes don't exist. I don't want to create duplicates according to the unique set of properties within the graph during the update.</p>
  
  <p>For example, I have People in the graph, and their uniqueness is determined by their first name and last name. The following code is to create two <em>distinct</em> people:</p>
  
  <pre><code>CREATE (p:Person{first:"barry",last:"smith",height:187});
  CREATE (p:Person{first:"fred",last:"jones",language:"welsh"});
  </code></pre>
  
  <p>Later, from one of the data sources I receive the following data records (one per line):</p>
  
  <pre><code>first: "fred", last: "lake", height: 201
  first: "barry", last: "smith", language: "english"
  first: "fred", last: "jones", language: "welsh", height: 188
  first: "fred", last: "jones", eyes: "brown"
  first: "barry", last: "smith"
  </code></pre>
  
  <p>After updating the graph I want to have the following nodes:</p>
  
  <pre><code>(:Person{first:"fred",last:"jones",language:"welsh",height:"188,eyes:"brown"})
  (:Person{first:"barry",last:"smith",language"english",height:187})
  (:Person{first:"fred",last"lake",height:201})
  </code></pre>
  
  <p>I'm trying to formulate a <code>MERGE</code> query which can do this kind of update. I have come up with the following approach:</p>
  
  <ul>
  <li>Start with a <code>MERGE</code> that uses the uniqueness properties (<code>first</code> and <code>last</code> from the example) to find or create the initial node;</li>
  <li>Then do a <code>SET</code> containing each property defined in the incoming record.</li>
  </ul>
  
  <p>So, for the three examples records given above:</p>
  
  <pre><code>MERGE (p:Person{first:"fred",last:"lake"}) SET p.height = 201;
  MERGE (p:Person{first:"barry",last:"smith"}) SET p.language = "english";
  MERGE (p:Person{first:"fred",last:"jones"}) SET p.language = "welsh", p.height = 188;
  MERGE (p:Person{first:"fred",last:"jones"}) SET p.eyes = "brown";
  MERGE (p:Person{first:"barry",last:"smith"});
  </code></pre>
  
  <p>I've tried this out and it works, but I'm curious to know whether this is the best way (most efficient...) to ensure uniqueness in the nodes based on a set of properties, and allow additional information to be added (or not) as updates come in over time?</p>