In the past three lessons you flew through tasks big and small. By now you should feel confident in being able to return any data you wish in almost any format supported. But what about modifying properties, or creating nodes and edges? In this lesson you’ll learn about SET, CREATE and MERGE. These clauses will help in your everyday endeavors.

In the first few lessons we used a simple data model which had City nodes connected by Road edges. Every city had only one property: name. Let’s change that! Add away to your heart’s desire.

MATCH (london:City {name: "London"})
SET london += {
  population: 8961989,
  area: 1572,
  timezone: "UTC",
  website: "www.london.gov.uk",
  capital: true
}

In this instance we added multiple properties to the city of London. Don’t worry, nobody expects one to go node by node and manually enter data. This can be easily done with a small python script and some Wikipedia scraping!

On to the next clause. CREATE is used to create nodes or edges. Let’s add flights to our network.

MATCH (london:City {name: "London"})
MATCH (paris:City {name: "Paris"})
CREATE (london)-[:Flight {length: 344}]->(paris)
CREATE (paris)-[:Flight {length: 344}]->(london)

But how does CREATE know what we want to create. The database sees london and paris variables already exist, so it only creates the edges between them. In a case where we aren’t sure if the node exists or not we can use MERGE instead of MATCH. MERGE will try to match the criteria we’re searching for, but if it fails to find it will create a new node that satisfies our query.

MATCH (london:City {name: "London"})
MERGE (mars:Planet {name: "Mars"})
CREATE (london)-[:Rocket]->(mars)

Unfortunately the playground doesn’t support modifying data, but don’t worry! This is a great opportunity to download a graph database and get comfortable with the ecosystem. The next lesson is the last one in the series. It talks about indexing and constraints. These concepts are very useful when you’re trying to create a new dataset! They significantly improve performance and keep your data clean.

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.