joe
joe

Reputation: 37

Neo4j add properties or attibutes to nodes when you create them

I want to create a node with about five other attributes including name and some characteristics to that node. How would I go about adding those attributes or properties in the create statement

CREATE (n {<node-name>:<label-name>})

For example if I wanted to create a node for a person I would name it after the person and have attributes name, weight, height, and etc. Is there any way to put all those in one create statement?

Upvotes: 3

Views: 2066

Answers (1)

Christophe Willemsen
Christophe Willemsen

Reputation: 20175

The best way to handle this is to use SET :

CREATE (n:NodeLabel) SET n += $props RETURN n

Where $props is a parameters map you pass as query parameter.

If you want to do it in the browser, you can do it by just creating the properties one by one :

CREATE (n:NodeLabel) SET n.name = "name", n.weight=88, n.height=107 RETURN n

Alternatively, you can also set them manually inside the node :

CREATE (n:NodeLabel {name:"cool", weight:88}) RETURN n

Thirdly, as mentioned by Nicole, you can set directly a map :

CREATE n SET n = {name:"Nicole", age:24} RETURN n

The Nicole's suggestion makes me think also that you can simulate the parameters using a WITH in upfront :

WITH {name:"Nicole",age:24} as params
CREATE n SET n = params
RETURN n

Upvotes: 7

Related Questions