首页 > 代码库 > Neo4j-cypher语言

Neo4j-cypher语言

1、创建节点

CREATE (ee:Person { name: "Emil", from: "Sweden", klout: 99 });

2、查询节点

MATCH (ee:Person) WHERE ee.name = "Emil" RETURN ee;

3、创建节点的关系

MATCH (w:Person),(m:Student) where id(w)=1 and id(m)=2 create (w)-[:IS_STUDENT]->(m);

4、一起创建节点和关系

MATCH (ee:Person) WHERE ee.name = "Emil"
CREATE (js:Person { name: "Johan", from: "Sweden", learn: "surfing" }),
(ir:Person { name: "Ian", from: "England", title: "author" }),
(rvb:Person { name: "Rik", from: "Belgium", pet: "Orval" }),
(ally:Person { name: "Allison", from: "California", hobby: "surfing" }),
(ee)-[:KNOWS {since: 2001}]->(js),(ee)-[:KNOWS {rating: 5}]->(ir),
(js)-[:KNOWS]->(ir),(js)-[:KNOWS]->(rvb),
(ir)-[:KNOWS]->(js),(ir)-[:KNOWS]->(ally),
(rvb)-[:KNOWS]->(ally);

5、查询节点及关系

MATCH (ee:Person)-[:KNOWS]-(friends)
WHERE ee.name = "Emil" RETURN ee, friends

6、复杂查询节点及关系

MATCH (js:Person)-[:KNOWS]-()-[:KNOWS]-(surfer)
WHERE js.name = "Johan" AND surfer.hobby = "surfing"
RETURN DISTINCT surfer;

7、节点关系深度查询(0..1代表深度几级)

MATCH (js:Person)-[*0..1]-(m) RETURN js

8、MERGE(先查询,当不存在时创建)

MERGE(ee:Person{name:"Emil"})  CREATE (ee:Person { name: "Emil", from: "Sweden", klout: 99 });

9、删除

DELETE n, r(删除节点,带关系的删除不了)

DETACH DELETE n(删除节点,带关系的直接删除)

MATCH (n) DETACH DELETE n(删除所有)

 

Neo4j-cypher语言