Creating Nodes
About this module
If you have taken the course Querying with Neo4j 4.x , 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. In this module, you will learn how to add data to a graph by creating nodes.
At the end of this module, you will write Cypher statements to:
-
Create a node.
-
Add and remove node labels.
-
Add and remove node properties.
-
Update node properties.
Because the code examples in this lesson modify the database, it is recommended that you do not execute them against your database as you will be doing so in the hands-on exercises. |
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:
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.
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:
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 thanCREATE
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 covered in the course, Using Indexes and Query Best Practices in Neo4j 4.x..
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:
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:
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 vice versa.
When specifying 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 propertyNameX 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:
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:
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
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.
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:
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:
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 an 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 is to set the property to null. And in another way, we use the REMOVE
clause.
Exercise 9: Creating nodes
Prior to performing this exercise, set up your development environment to use one of the following, which is covered in the course, Overview of Neo4j 4.x. |
-
Neo4j Desktop
-
Neo4j Sandbox
-
Neo4j Aura
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.
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 can now write Cypher statements to:
-
Create a node.
-
Add and remove node labels.
-
Add and remove node properties.
-
Update node properties.
Need help? Ask in the Neo4j Community