问题
I'd like to recreate a whole chain of nodes and relationships in Neo4j.
I know that I can save a path with p=
, but CREATE
doesn't accept this.
MATCH p=(:Person)-[*]->(:Dog) CREATE p
Is there a way to do this?
回答1:
As you've seen, the syntax you tried does not work, so one would have to attempt to write a lot of Cypher code to copy all the parts of each path.
However, it turns out that it is not currently possible to use Cypher alone to duplicate arbitrary paths -- as some needed capabilities are missing.
For example, although you can use the LABELS()
function to get a collection containing an existing node's labels, there is no way to create/set another node's label from that data. Cypher only supports hardcoded labels (when creating a node, as in CREATE (n:Foo)
; and when updating a node, as in SET n:Foo
). A similar consideration applies to relationship types.
回答2:
WARNING: One would usually create additional relationships to nodes rather than duplicate nodes in a graph database.
This is not strictly Cypher, but works in the Neo4j shell. It should work as of Neo4j 2.0.0. A word of caution: this is listed as an experimental and incomplete feature.
dump MATCH (pers:Person)-[r*:OWNS]->(d:Dog) RETURN pers,r,d;
You can directly execute the returned output to duplicate the nodes. IDs will be changed if they already exist. The nodes and relationships will be created with the same labels and properties.
This is super handy for backing up a database.
More info on the dump command
来源:https://stackoverflow.com/questions/35586224/how-can-i-copy-a-path-in-neo4j