16.17. Create

16.17.1. Create single node
16.17.2. Create single node and set properties
16.17.3. Return created node
16.17.4. Create a relationship between two nodes
16.17.5. Create a relationship and set properties
16.17.6. Create single node from map
16.17.7. Create multiple nodes from maps

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

16.17.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
0 ms

(empty result)


16.17.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
2 ms

(empty result)


16.17.3. 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, which fits with how RELATE looks.

Result

a
1 row
Nodes created: 1
Properties set: 1
1 ms

Node[1]{name->"Andres"}


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

START a=node(1), b=node(2)
CREATE a-[r:REL]->b
RETURN r

The created relationship is returned.

Result

r
1 row
Relationships created: 1
2 ms

:REL[0] {}


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

START a=node(1), b=node(2)
CREATE a-[r:REL {name : a.name + '<->' + b.name }]->b
RETURN r

The newly created relationship is returned.

Result

r
1 row
Relationships created: 1
Properties set: 1
2 ms

:REL[0] {name->"Andres<->Michael"}


16.17.6. 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 node {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 );

16.17.7. 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 node {props}

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);