CREATE
The
CREATE
clause is used to create nodes and relationships.
In the |
Create nodes
Create single node
Creating a single node is done by issuing the following query:
CREATE (n)
|
Rows: 0 |
Create multiple nodes
Creating multiple nodes is done by separating them with a comma.
CREATE (n), (m)
|
Rows: 0 |
Create a node with a label
To add a label when creating a node, use the syntax below:
CREATE (n:Person)
|
Rows: 0 |
Create a node with multiple labels
To add labels when creating a node, use the syntax below. In this case, we add two labels.
CREATE (n:Person:Swedish)
|
Rows: 0 |
Create relationships
Create a relationship between two nodes
To create a relationship between two nodes, we first get the two nodes. Once the nodes are loaded, we simply create a relationship between them.
MATCH
(a:Person),
(b:Person)
WHERE a.name = 'A' AND b.name = 'B'
CREATE (a)-[r:RELTYPE]->(b)
RETURN type(r)
The created relationship is returned by the query.
type(r) |
---|
|
Rows: 1 |
Create a relationship and set properties
Setting properties on relationships is done in a similar manner to how it’s done when creating nodes. Note that the values can be any expression.
MATCH
(a:Person),
(b:Person)
WHERE a.name = 'A' AND b.name = 'B'
CREATE (a)-[r:RELTYPE {name: a.name + '<->' + b.name}]->(b)
RETURN type(r), r.name
The type and name of the newly-created relationship is returned by the example query.
type(r) | r.name |
---|---|
|
|
Rows: 1 |
Create a full path
When you use CREATE
and a pattern, all parts of the pattern that are not already in scope at this time will be created.
CREATE p = (:Person {name:'Andy'})-[:WORKS_AT]->(:Company {name: 'Neo4j'})<-[:WORKS_AT]-(:Person {name: 'Michael'})
RETURN p
This query creates three nodes and two relationships in one go, assigns it to a path variable, and returns it.
p |
---|
|
Rows: 1 |
Use parameters with CREATE
Create node with a parameter for the properties
You can also create a graph entity from a map.
All the key/value pairs in the map will be set as properties on the created relationship or node.
In this case we add a Person
label to the node as well.
{
"props": {
"name": "Andy",
"position": "Developer"
}
}
CREATE (n:Person $props)
RETURN n
n |
---|
|
Rows: 1 |
Create multiple nodes with a parameter for their properties
By providing Cypher® an array of maps, it will create a node for each map.
{
"props": [ {
"name": "Andy",
"position": "Developer"
}, {
"name": "Michael",
"position": "Developer"
} ]
}
UNWIND $props AS map
CREATE (n)
SET n = map
|
Rows: 0 |
Was this page helpful?