13.1. Create

13.1.1. Create single node
13.1.2. Create single node and set properties
13.1.3. Create node and add labels
13.1.4. Create node and add labels and properties
13.1.5. Return created node
13.1.6. Create a relationship between two nodes
13.1.7. Create a relationship and set properties
13.1.8. Create a full path
13.1.9. Create single node from map
13.1.10. Create multiple nodes from maps

Creating graph elements — nodes and relationships, is done with CREATE.

[Tip]Tip

In the CREATE clause, patterns are used a lot. Read Section 11.5, “Patterns” for an introduction.

13.1.1. Create single node

Creating a single node is done by issuing the following query.

Query. 

CREATE (n)

Nothing is returned from this query, except the count of affected nodes.

Result

Nodes created: 1

(empty result)


13.1.2. Create single node and set properties

The values for the properties can be any scalar expressions.

Query. 

CREATE (n {name : 'Andres', title : 'Developer'})

Nothing is returned from this query.

Result

Nodes created: 1
Properties set: 2

(empty result)


13.1.3. Create node and add labels

To add labels to the newly created node, you can either use LABEL followed by an expression,or you can use the labels short form syntax.

Query. 

CREATE (n:Person)

Nothing is returned from this query.

Result

Nodes created: 1
Labels added: 1

(empty result)


13.1.4. Create node and add labels and properties

When creating a new node with labels, you can add properties using VALUES at the same time

Query. 

CREATE (n:Person {name : 'Andres', title : 'Developer'})

Nothing is returned from this query.

Result

Nodes created: 1
Properties set: 2
Labels added: 1

(empty result)


13.1.5. Return created node

Creating a single node is done by issuing the following query.

Query. 

CREATE (a {name : 'Andres'})
RETURN a

The newly created node is returned. This query uses the alternative syntax for single node creation.

Result

a
1 row
Nodes created: 1
Properties set: 1

Node[2]{name:"Andres"}


13.1.6. 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.

Query. 

MATCH a:Person, b:Person
WHERE a.name = 'Node A' and b.name = 'Node B'
CREATE a-[r:RELTYPE]->b
RETURN r

The created relationship is returned by the query.

Result

r
1 row
Relationships created: 1

:RELTYPE[1] {}


13.1.7. 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.

Query. 

MATCH a:Person, b:Person
WHERE a.name = 'Node A' and b.name = 'Node B'
CREATE a-[r:RELTYPE {name : a.name + '<->' + b.name }]->b
RETURN r

The newly created relationship is returned by the example query.

Result

r
1 row
Relationships created: 1
Properties set: 1

:RELTYPE[1] {name:"Node A<->Node B"}


13.1.8. 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.

Query. 

CREATE p = (andres {name:'Andres'})-[:WORKS_AT]->neo<-[:WORKS_AT]-(michael {name:'Michael'})
RETURN p

This query creates three nodes and two relationships in one go, assigns it to a path identifier, and returns it.

Result

p
1 row
Nodes created: 3
Relationships created: 2
Properties set: 2

[Node[4]{name:"Andres"},:WORKS_AT[2] {},Node[5]{},:WORKS_AT[3] {},Node[6]{name:"Michael"}]


13.1.9. Create single node from map

You can also create a graph entity from a Map<String,Object> map. All the key/value pairs in the map will be set as properties on the created relationship or node.

Query. 

create ({props})

This query can be used in the following fashion:

Map<String, Object> props = new HashMap<String, Object>();
props.put( "name", "Andres" );
props.put( "position", "Developer" );

Map<String, Object> params = new HashMap<String, Object>();
params.put( "props", props );
engine.execute( "create ({props})", params );

13.1.10. Create multiple nodes from maps

By providing an iterable of maps (Iterable<Map<String,Object>>), Cypher will create a node for each map in the iterable. When you do this, you can’t create anything else in the same create statement.

Query. 

create (n {props}) return n

This query can be used in the following fashion:

Map<String, Object> n1 = new HashMap<String, Object>();
n1.put( "name", "Andres" );
n1.put( "position", "Developer" );

Map<String, Object> n2 = new HashMap<String, Object>();
n2.put( "name", "Michael" );
n2.put( "position", "Developer" );

Map<String, Object> params = new HashMap<String, Object>();
List<Map<String, Object>> maps = Arrays.asList( n1, n2 );
params.put( "props", maps );
engine.execute( "create (n {props}) return n", params );