cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Node names aren't working

I am new to neo4j, and am just following the example from the docs:

neo4j@neo4j> CREATE (john:Person {name: 'John'})
CREATE (joe:Person {name: 'Joe'})
CREATE (steve:Person {name: 'Steve'})
CREATE (sara:Person {name: 'Sara'})
CREATE (maria:Person {name: 'Maria'});
0 rows available after 55 ms, consumed after another 0 ms
Added 5 nodes, Set 5 properties, Added 5 labels

neo4j@neo4j> MATCH (n) RETURN n;
+---------------------------+
| n |
+---------------------------+
| (:Person {name: "John"}) |
| (:Person {name: "Joe"}) |
| (:Person {name: "Steve"}) |
| (:Person {name: "Sara"}) |
| (:Person {name: "Maria"}) |
+---------------------------+

5 rows available after 16 ms, consumed after another 5 ms

Note that there are no node names here. So when I try to create relationships, it can't find the nodes and creates new (blank) nodes:

neo4j@neo4j> CREATE (john)-[:FRIEND]->(joe)-[:FRIEND]->(steve)
CREATE (john)-[:FRIEND]->(sara)-[:FRIEND]->(maria);
0 rows available after 39 ms, consumed after another 0 ms
Added 5 nodes, Created 4 relationships
neo4j@neo4j> MATCH (n) RETURN n;
+---------------------------+
| n |
+---------------------------+
| (:Person {name: "John"}) |
| (:Person {name: "Joe"}) |
| (:Person {name: "Steve"}) |
| (:Person {name: "Sara"}) |
| (:Person {name: "Maria"}) |
| () |
| () |
| () |
| () |
| () |
+---------------------------+
10 rows available after 2 ms, consumed after another 1 ms

I can see in the web browser that the blank nodes are all connected, and the named nodes are unconnected.

neo4j@neo4j> call dbms.components() yield name, versions, edition unwind versions as version return name, version,
edition;
+----------------------------------------+
| name | version | edition |
+----------------------------------------+
| "Neo4j Kernel" | "4.2.1" | "community" |
+----------------------------------------+

Any help would be greatly appreciated.

4 REPLIES 4

ameyasoft
Graph Maven
CREATE (joe:Person {name: 'Joe'})

Here:
 'joe' is an alias name and you can have any name
'Person' is the node name (or node label)
 This is what you see when you ran MATCH(n) RETURN n

Thanks. I think I see the problem now. The CREATE relationships have to be done in the same statement as CREATE nodes (where the aliases are introduced).

Hi @ben5,

Few suggestions,

  1. Instead of CREATE, replace with MERGE. CREATE will always create new nodes everything time you execute the statement, even if it has the same properties. With MERGE, Neo4j checks if the node with the properties already exists. If it exists it won't create a new one, if it doesn't exist it will create a new node.

CREATE -> I executed Create 2 times with the same properties, and the result is 2 nodes

CREATE (john:Person {name: 'John'});
Added 1 label, created 1 node, set 1 property, completed after 3 ms.

CREATE (john:Person {name: 'John'});
Added 1 label, created 1 node, set 1 property, completed after 3 ms.

match (n) return (n)

╒═══════════════╕
│"n"            │
╞═══════════════╡
│{"name":"John"}│
├───────────────┤
│{"name":"John"}│
└───────────────┘

MERGE -> I execute MERGE with the same properties, but no duplicate nodes are created.

MERGE (john:Person {name: 'John'})
Added 1 label, created 1 node, set 1 property, completed after 3 ms.

MERGE (john:Person {name: 'John'})
(no changes, no records)

MERGE (john:Person {name: 'John'})
(no changes, no records)

MERGE (john:Person {name: 'John'})
(no changes, no records)

match (n) return (n)
╒═══════════════╕
│"n"            │
╞═══════════════╡
│{"name":"John"}│
└───────────────┘
  1. You can also have all of them in a single transactions, -
MERGE (john:Person {name: 'John'})
MERGE (joe:Person {name: 'Joe'})
MERGE (steve:Person {name: 'Steve'})
MERGE (sara:Person {name: 'Sara'})
MERGE (maria:Person {name: 'Maria'})
MERGE (john)-[:FRIEND]->(joe)-[:FRIEND]->(steve)
MERGE (john)-[:FRIEND]->(sara)-[:FRIEND]->(maria);

2X_b_bd4fee2f58ab5f4e77d25630c378468e959fdaac.png

Just make sure the "variable" names are matched within a single transactions.

Hope this works ...

Here WITH is not required. It works even without WITH.
WITH is required if a MERGE statement follows a MATCH statement.

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

All the sessions of the conference are now available online