title: How to create a node (if not exists) with in Neo4j
body: <p>I am trying to make a system where an admin can push a <code>blogPost</code> to the front page. I use Neo4j to save the Mongo ID's of these objects (not the best way but I'm forced by school to use both Neo4j and MongoDB). </p>
  
  <p>Now I have made a query and it works, it saves the admin, the blogpost, and the <code>added_by</code> relation. The problem is that it creates the admin, blogpost whether or not they already exist. How can I solve this? My graph is now a mess.</p>
  
  <p><a href="https://i.stack.imgur.com/JKvms.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JKvms.png" alt="Sample graph"></a></p>
  
  <p>This is my Node.js route and query:</p>
  
  <pre><code>routes.post('/blogPosts/frontpage/:id/:aid', function (req, res) {
    const id = req.param('id');
    const aid = req.param('aid');
  
    session
      .run("CREATE(n:BlogPost {mongoId:{idNeo}}), (b:Admin {mongoAdminId:{idNeoAd}})", {idNeo: id, idNeoAd: aid});
      session
      .run("MATCH (u:BlogPost {mongoId:{idNeo}}), (r:Admin {mongoAdminId:{idNeoAd}}) CREATE (u)-[:ADDED_BY]-&gt;(r)", {idNeo: id, idNeoAd: aid})
      .then(function(result) {
        res.status(200).json({"response": "BlogPost added to front page."});
        session.close();
      })
      .catch((error) =&gt; {
        res.status(400).json(error);
      });
  });
  </code></pre>
  
  <p>How can I change my route to only add the admin/post if they don't exist yet?</p>
  
  <p><strong>EDIT:</strong> </p>
  
  <pre><code>session.run("CREATE (u:BlogPost {mongoId:{idNeo}}), (r:Admin {mongoAdminId:{idNeoAd}}) CREATE UNIQUE(u)-[:ADDED_BY]-&gt;(r)", {idNeo: id, idNeoAd: aid})
  </code></pre>
  
  <p>This fixes the mess but still creates the nodes even if they exist.</p>
  
  <p>Now I get a semantic error in my query (in the session.run)</p>
  
  <pre><code>routes.post('/blogPosts/frontpage/:id/:aid', function (req, res) {
    const id = req.param('id');
    const aid = req.param('aid');
  
      session
      .run("MERGE (n:BlogPost {mongoId:{idNeo}}) MERGE (b:Admin {mongoAdminId:{idNeoAd}}) CREATE UNIQUE(u)-[:ADDED_BY]-&gt;(r)", {idNeo: id, idNeoAd: aid})
      .then(function(result) {
        res.status(200).json({"response": "BlogPost added to front page."});
        session.close();
      })
      .catch((error) =&gt; {
        res.status(400).json(error);
      });
  });
  </code></pre>