Online Course Introduction to Neo4j 4.0 Neo4j is a Graph Database The Neo4j Graph Platform Introduction to Cypher Using WHERE to Filter Queries Working with Patterns in Queries Working with Cypher Data Controlling the Query Chain Controlling Results Returned Creating… Read more →

Creating Nodes

About this module

Using an existing dataset, you have learned how to query the graph with both simple and complex patterns, how to control processing during a query, and how to control how results are returned. Next, you will learn how to add data to a graph by creating nodes.

At the end of this module, you should be able to write Cypher statements to:

  • Create a node.
  • Add and remove node labels.
  • Add and remove node properties.
  • Update node properties.

Syntax: Creating nodes

Recall that a node is an element of a graph representing a domain entity that has zero or more labels, properties, and relationships to or from other nodes in the graph.

When you create a node, you can add it to the graph without connecting it to another node. In virtually all cases, the nodes that you create in a graph will be connected to other nodes and you will learn about creating these connections (relationships) in the next lesson of this training.

Here is the simplified syntax for creating a node:

CREATE (optionalVariable optionalLabels {optionalProperties})

If you plan on referencing the newly-created node, you must provide a variable. Whether you provide labels or properties at node creation time is optional. In most cases, you will want to provide some label and property values for a node when created. This will enable you to later retrieve the node. Provided you have a reference to the node (for example, using a MATCH clause), you can always add, update, or remove labels and properties at a later time.

Next, you will see some examples of creating a single node in Cypher.

Example #1

Add a node to the graph of type Movie with the title, Batman Begins. This node can be retrieved using the title. A set of nodes with the label Movie can also be retrieved which will contain this node:

CREATE (:Movie {title: 'Batman Begins'})

Example #2

Add a node with two labels to the graph of types Movie and Action with the title, Batman Begins. This node can be retrieved using the title. A set of nodes with the labels Movie or Action can also be retrieved which will contain this node:

CREATE (:Movie:Action {title: 'Batman Begins'})

Example #3

Add a node to the graph of types Movie and Action with the title, Batman Begins. This node can be retrieved using the title. A set of nodes with the labels Movie or Action can also be retrieved which will contain this node. The variable m can be used for later processing after the CREATE clause:

CREATE (m:Movie:Action {title: 'Batman Begins'})

Example #4

Add a node to the graph of types Movie and Action with the title Batman Begins. This node can be retrieved using the title. A set of nodes with the labels Movie or Action can also be retrieved which will contain this node. Here we return the title of the node:

CREATE (m:Movie:Action {title: ' Batman Begins'})
RETURN m.title

Result of creating node

Here is what you see in Neo4j Browser when you create a node for the movie, Batman Begins where we use the variable, m to return its title property value:

CreateBatmanBegins

Here is the node when we retrieve it. It is the only node in the graph with a label of Action so to retrieve it from the graph is straightforward where we only specify the node labels.

RetrieveBatmanBegins

When the graph engine creates a node, it automatically assigns a read-only, unique ID to the node. Here we see that the id of the node is 171. This is not a property of a node, but rather an internal value.

After you have created a node, you can add more properties or labels to it and most importantly, connect it to another node.

Creating multiple nodes

You can create multiple nodes by simply separating the nodes specified with commas, or by specifying multiple CREATE statements.

Here is an example, where we create some Person nodes that will represent some of the people associated with the movie Batman Begins:

CREATE
(:Person {name: 'Michael Caine', born: 1933}),
(:Person {name: 'Liam Neeson', born: 1952}),
(:Person {name: 'Katie Holmes', born: 1978}),
(:Person {name: 'Benjamin Melniker', born: 1913})

Here is the result of running this Cypher statement:

CreateMultiplePersonNodes

Note

The graph engine will create a node with the same properties of a node that already exists. You can prevent this from happening in one of two ways:

  • You can use MERGE rather than CREATE when creating the node.
  • You can add constraints to your graph.

You will learn about merging data later in this course. Constraints are configured globally for a graph and are also covered later in this course.

Syntax: Adding labels to a node

You may not know ahead of time what label or labels you want for a node when it is created. You add labels to a node using the SET clause.

Here is the simplified syntax for adding labels to a node:

SET x:Label         // adding one label to node referenced by the variable x
SET x:Label1:Label2	// adding two labels to node referenced by the variable x

If you attempt to add a label to a node for which the label already exists, the SET processing is ignored.

Example: Adding labels to a node

Here is an example where we add the Fantasy label to the node that has a labels, Movie and Action:

MATCH (m:Movie)
WHERE m.title = 'Batman Begins'
SET m:Fantasy
RETURN labels(m)

Assuming that we have previously created the node for the movie, here is the result of running this Cypher statement:

SetFantasyLabel

Notice here that we call the built-in function, labels() that returns the set of labels for the node.

Syntax: Removing labels from a node

Perhaps your data model has changed or the underlying data for a node has changed so that the label for a node is no longer useful or valid.

Here is the simplified syntax for removing a label from a node:

// remove the label from the node referenced by the variable x
REMOVE x:Label

Here is the simplified syntax for removing multiple labels from a node:

// remove the two labels from the node referenced by the variable x
REMOVE x:Label1, x:Label2

If you attempt to remove a label from a node for which the label does not exist, it is ignored.

Example: Removing labels from a node

Here is an example where we remove the Action and Fantasy labels from the node that has the label, Action:

MATCH (m:Action)
REMOVE m:Action, m:Fantasy
RETURN labels(m)

Assuming that we have previously created the node for the movie, here is the result of running this Cypher statement:

RemoveActionLabel

Syntax: Adding properties to a node

After you have created a node and have a reference to the node, you can add properties to the node using the SET keyword.

Here are simplified syntax examples for adding properties to a node referenced by the variable x:

SET x.propertyName = value
SET x.propertyName1 = value1	, x.propertyName2 = value2
SET x = {propertyName1: value1, propertyName2: value2}
SET x += {propertyName1: value1, propertyName2: value2}

If the property does not exist, it is added to the node. If the property exists, its value is updated. If the value specified is null, the property is removed.

Note that the type of data for a property is not enforced. That is, you can assign a string value to a property that was once a numeric value and visa versa.

When specify the JSON-style object for assignment (using =) of the property values for the node, the object must include all of the properties and their values for the node as the existing properties for the node are overwritten. However, if you specify += when assigning to a property, the value at valueX is updated if propertyNnameX exists for the node. If propertyNameX does not exist for the node, then the property is added to the node.

Example: Adding properties to a node

Here is an example where we add the properties released and lengthInMinutes to the movie Batman Begins:

MATCH (m:Movie)
WHERE m.title = 'Batman Begins'
SET m.released = 2005, m.lengthInMinutes = 140
RETURN m

Assuming that we have previously created the node for the movie, here is the result of running this Cypher statement:

AddReleasedMinutesProperties

Example 2: Adding properties to node

Here is another example where we set the property values to the movie node using the JSON-style object containing the property keys and values. Note that all properties must be included in the object.

MATCH (m:Movie)
WHERE m.title = 'Batman Begins'
SET  m = {title: 'Batman Begins',
          released: 2005,
          lengthInMinutes: 140,
          videoFormat: 'DVD',
          grossMillions: 206.5}
RETURN m

Here is the result of running this Cypher statement:

SetPropertiesObject

Note that when you add a property to a node for the first time in the graph, the property key is added to the graph. So for example, in the previous example, we added the videoFormat and grossMillions property keys to the graph as they have never been used before for a node in the graph. Once a property key is added to the graph, it is never removed.

Viewing property keys

AllPropertyKeys1

When you examine the property keys in the database (by executing CALL db.propertyKeys(), you will see all property keys created for the graph, regardless of whether they are currently used for nodes and relationships.

AllPropertyKeys2

Retrieving properties of a node

In addition to querying the names of all properties, you can also return the properties of a specific node. You can retrieve the properties of a node as a Cypher map:

MATCH (m:Movie)
WHERE m.title = 'Batman Begins'
RETURN properties(m)

Here is the result returned:

PropertyKeysBatmanBegins

Updating properties – JSON-style

Here is an example where we use the JSON-style object to add the awards property to the node and update the grossMillions property:

MATCH (m:Movie)
WHERE m.title = 'Batman Begins'
SET  m += { grossMillions: 300,
            awards: 66}
RETURN m

Here is the result:

AddAndUpdateProperties

Syntax: Removing properties from a node

There are two ways that you can remove a property from a node. One way is to use the REMOVE keyword. The other way is to set the property’s value to null.

Here are simplified syntax examples for removing properties from a node referenced by the variable x:

REMOVE x.propertyName
SET x.propertyName = null

Example: Removing properties from a node

Suppose we determined that no other Movie node in the graph has the properties, videoFormat and grossMillions. There is no restriction that nodes of the same type must have the same properties. However, we have decided that we want to remove these properties from this node. Here is example Cypher to remove this property from this Batman Begins node:

MATCH (m:Movie)
WHERE m.title = 'Batman Begins'
SET m.grossMillions = null
REMOVE m.videoFormat
RETURN m

Assuming that we have previously created the node for the movie with the these properties, here is the result of running this Cypher statement where we remove each property a different way. One way we remove the property using the SET clause to set the property to null. And in another way, we use the REMOVE clause.

RemoveProperties

Exercise 9: Creating nodes

In the query edit pane of Neo4j Browser, execute the browser command:

:play 4.0-intro-neo4j-exercises

and follow the instructions for Exercise 9.

Note
This exercise has 18 steps. Estimated time to complete: 30 minutes.

Check your understanding

Question 1

What Cypher clauses can you use to create a node?

Select the correct answers.

  • CREATE
  • CREATE NODE
  • MERGE
  • ADD

Question 2

Suppose that you have retrieved a node, s with a property, color. What Cypher clause do you add here to delete the color property from this node?

MATCH (s:Shape {location: [20,30]})
???
RETURN s

Select the correct answers.

  • DELETE s.color
  • SET s.color=null
  • REMOVE s.color
  • SET s.color=?

Question 3

Suppose you have this Person node in the graph: “name”: “Joe Cool”, “birthYear”: 1985. What properties have values after executing this code?

MATCH (p:Person) WHERE p.name = 'Joe Cool'
SET  p += { salary: 130000,
            department: 'Engineering'}
REMOVE p.birthYear

Select the correct answers.

  • name
  • birthYear
  • salary
  • department

Summary

You should now be able to write Cypher statements to:

  • Create a node.
  • Add and remove node labels.
  • Add and remove node properties.
  • Update node properties.

Stay Connected

Sign up to find out more about Neo4j's upcoming events & meetups.