Neo4j Note4:Neo4j使用

Neo4j Note4:Neo4j使用

命令

  • 编写Cypher命令,简称CQL
CQL命令
用法
CREATE
创建节点、关系和属性
MATCH
检索有关节点,关系和属性数据
RETURN
返回查询结果
WHERE
t提供条件过滤检索数据
DELETE
删除节点和关系数据
REMOVE
删除节点和关系的属性
ORDER BY
排序检索数据
SET
添加和更新标签

创建节点

CREATE (n:Person {name:'hengao'}) RETURN n
  • CREATE是创建操作,Person是标签,代表节点的类型。花括号{}代表节点的属性,属性类似Python的字典。这条语句的含义就是创建一个标签为Person的节点,该节点具有一个name属性,属性值是hengao
notion image

创建更多的人物节点

CREATE (n:Person {name:'john'}) RETURN n; CREATE (n:Person {name:'Sally'}) RETURN n; CREATE (n:Person {name:'Steve'}) RETURN n; CREATE (n:Person {name:'Mike'}) RETURN n; CREATE (n:Person {name:'Liz'}) RETURN n; CREATE (n:Person {name:'Shawn'}) RETURN n;

创建地区节点

CREATE (n:Location {city:'Miami', state:'FL'}); CREATE (n:Location {city:'Boston', state:'MA'}); CREATE (n:Location {city:'Lynn', state:'MA'}); CREATE (n:Location {city:'Portland', state:'ME'}); CREATE (n:Location {city:'San Francisco', state:'CA'});

创建关系

MATCH (a:Person {name:'Liz'}), (b:Person {name:'Mike'}) MERGE (a)-[:FRIENDS]->(b)
  • 箭头 -->是有方向的,表示是从a到b的关系。
notion image

关系增加属性

MATCH (a:Person {name:'Shawn'}), (b:Person {name:'Sally'}) MERGE (a)-[:FRIENDS {since:2001}]->(b)
notion image

创建关系

MATCH (a:Person {name:'Shawn'}), (b:Person {name:'John'}) MERGE (a)-[:FRIENDS {since:2012}]->(b); MATCH (a:Person {name:'Mike'}), (b:Person {name:'Shawn'}) MERGE (a)-[:FRIENDS {since:2006}]->(b); MATCH (a:Person {name:'Sally'}), (b:Person {name:'Steve'}) MERGE (a)-[:FRIENDS {since:2006}]->(b); MATCH (a:Person {name:'Liz'}), (b:Person {name:'John'}) MERGE (a)-[:MARRIED {since:1998}]->(b);
notion image
 

建立不同节点之间的关系

  • 以人物和地点关系为例
MATCH (a:Person {name:'John'}), (b:Location {city:'Boston'}) MERGE (a)-[:BORN_IN {year:1978}]->(b)
notion image

创建更多人的出生地

MATCH (a:Person {name:'Liz'}), (b:Location {city:'Boston'}) MERGE (a)-[:BORN_IN {year:1981}]->(b); MATCH (a:Person {name:'Mike'}), (b:Location {city:'San Francisco'}) MERGE (a)-[:BORN_IN {year:1960}]->(b); MATCH (a:Person {name:'Shawn'}), (b:Location {city:'Miami'}) MERGE (a)-[:BORN_IN {year:1960}]->(b); MATCH (a:Person {name:'Steve'}), (b:Location {city:'Lynn'}) MERGE (a)-[:BORN_IN {year:1970}]->(b);
notion image

查询

  • 有在Boston出生的人物
MATCH (a:Person)-[:BORN_IN]->(b:Location {city:'Boston'}) RETURN a,b
notion image

查询所有对外有关系的节点

  • 📢📢📢注意这里箭头的方向,返回结果不含任何地区节点,因为地区并没有指向其他节点(只是被指向)
MATCH (a)-->() RETURN a
notion image

查询所有有关系的节点

MATCH (a)--() RETURN a
notion image

查询所有对外有关系的节点以及关系类型

MATCH (a)-[r]->() RETURN a.name, type(r)
notion image
 
 

查询所有指定关系的节点

  • 查询所有结婚关系的节点
MATCH (n)-[:MARRIED]-() RETURN n
notion image

创建节点的时候就建好关系

CREATE (a:Person {name:'Todd'})-[r:FRIENDS]->(b:Person {name:'Carlos'})
notion image

查找某人的朋友的朋友

  • 返回Mike的朋友的朋友:
MATCH (a:Person {name:'Mike'})-[r1:FRIENDS]-()-[r2:FRIENDS]-(friend_of_a_friend) RETURN friend_of_a_friend.name AS fofName
notion image

增加修改节点属性

  • SET表修改操作
MATCH (a:Person {name:'Liz'}) SET a.age=34; MATCH (a:Person {name:'Shawn'}) SET a.age=32; MATCH (a:Person {name:'John'}) SET a.age=44; MATCH (a:Person {name:'Mike'}) SET a.age=25;
notion image

删除节点的属性

# 1. 创捷属性 MATCH (a:Person {name:'Mike'}) SET a.test='test' # 2. 删除属性 MATCH (a:Person {name:'Mike'}) REMOVE a.test

删除有关系的节点

MATCH (a:Person {name:'Todd'})-[rel]-(b:Person) DELETE a,b,rel

删除节点

  • 删除节点前先删除节点的关系
MATCH (a:Location {city:'Portland'}) DELETE a

删除数据库中的图

  • 💥💥💥删除了所有
MATCH (n) DETACH DELETE n