简体   繁体   中英

neo4j relationship creating multiple nodes

I am new to neo4j and been trying things.

I created two nodes successfully

CREATE (sally:Person { name: 'Sally', age: 32 })
CREATE (john:Person { name: 'John', age: 27 })

With this command:

CREATE (sally)-[:FRIEND_OF { since: 1357718400 }]->(john)

ends up successful but creates two other nodes with different id s.

So, I end up with 4 nodes.

Identifiers for nodes and relationships are only meaningful within a single Cypher query. If you had combined your two queries into one, then sally and john would not have been re-created:

CREATE (sally:Person { name: 'Sally', age: 32 })
CREATE (john:Person { name: 'John', age: 27 })
CREATE (sally)-[:FRIEND_OF { since: 1357718400 }]->(john);

If you really needed to have 2 separate queries, your second query would have to find the sally and john nodes before re-using them. You new query could also use some different identifiers for them (like a and b ):

CREATE (sally:Person { name: 'Sally', age: 32 })
CREATE (john:Person { name: 'John', age: 27 })

MATCH (a:Person { name: 'Sally'}), (b:Person { name: 'John'})
CREATE (a)-[:FRIEND_OF { since: 1357718400 }]->(b);

In the second query, I assume that a Person node's name is sufficient to find a unique node.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM