CREATE (p:Person{name:"a",age:21}) return p CREATE (p:Person{name:"b",age:21}) return p CREATE (p:Person{name:"c",age:21}) return p
创建关系
1 2 3 4 5 6
MATCH (p1:Person {name: 'qxdn'}) MATCH (p2:Person {name: 'a'}) CREATE (p1)-[rel:IS_FRIENDS_WITH{from:2021}]->(p2) // OR 使用MERGE可以避免重复定义 MATCH (p1:Person {name:'b'}), (p2:Person {name:"qxdn"}) MERGE (p1)<-[:IS_FRIENDS_WITH{from:2021}]-(p2)
MATCH (p:Person{name:"qxdn"}) CREATE (p)-[:BORIN]->(:Country{name:"China"}) // CREATE (:Person{name:"f"}) -[:IS_FRIENDS_WITH{from:2020}]->(:Person{name:"g"})
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// 删除关系 MATCH (:Person {name: 'f'})-[r:IS_FRIENDS_WITH]->(:Person {name: 'g'}) DELETE r // 删除节点 MATCH (p:Person {name: 'c'}) DELETE p // 删除节点和关系 MATCH (p1:Person {name: 'd'})-[r:IS_FRIENDS_WITH]->(p2:Person {name: 'e'}) DELETE p1,p2,r // 断开一个节点的关系和其自身 MATCH (c:Country{name:"China"}) DETACH DELETE c // 删除一个节点的属性 MATCH (n:Person {name: 'qxdn'}) REMOVE n.age // OR MATCH (n:Person {name: 'a'}) SET n.age = null
查
先执行一下语句增加节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// CREATE (:Country{name:"China"}) // CREATE (:Country{name:"USA"}) // MATCH (p:Person{name:"qxdn"}),(c:Country{name:"China"}) MERGE (p)-[:BORN_IN]->(c) // MATCH (p:Person{name:"a"}),(c:Country{name:"China"}) MERGE (p)-[:BORN_IN]->(c) // MATCH (p:Person{name:"b"}),(c:Country{name:"USA"}) MERGE (p)-[:BORN_IN]->(c) // MATCH (p:Person{name:"g"}),(c:Country{name:"USA"}) MERGE (p)-[:BORN_IN]->(c) // MATCH (p:Person{name:"f"}),(c:Country{name:"China"}) MERGE (p)-[:BORN_IN]->(c)
查询语句主要靠 MATCH 关键词,其功能类似于 sql 中的 SELECT
查询所有节点,只输出 25 个避免过多节点。
1 2 3
MATCH (n) RETURN n LIMIT 25
结果与上图一样
查询 qxdn 出生的国家
1 2
MATCH (:Person {name: 'qxdn'})-[:BORN_IN]->(c:Country) RETURN c
查询 qxdn 和 a 做朋友的起始年
1 2
MATCH (:Person {name: 'qxdn'})-[r:IS_FRIENDS_WITH]->(:Person {name: 'a'}) RETURN r.from
查询所有中国出生的人
1 2
MATCH (p:Person)-[:BORN_IN]->(c:Country{name:"China"}) RETURN p,c
改
改主要是使用 SET 关键词
修改 a 的出生国
1 2 3
MATCH (p:Person{name:"a"})-[:BORN_IN]-(c:Country{name:"China"}) SET c.name = "USA" RETURN p,c