四 图数据库的创建
1 创建一个节点 Person {name: "My Name"}
CREATE (me:Person {name: "My Name"}) return me;
CREATE (movie:Movie {title: "Mystic River", released:1993})
RETURN movie;
2修改属性
更改属性 : movie.tagline = "We bury our sins here, Dave. We wash them clean."
MATCH (movie:Movie)
WHERE movie.title="Mystic River"
SET movie.tagline = "We bury our sins here, Dave. We wash them clean."
RETURN movie;
更改 : movie.released = 2003
MATCH (movie:Movie)
WHERE movie.title="Mystic River"
SET movie.released = 2003
RETURN movie;
3 增加关系-边
MATCH (me:Person), (movie:Movie)
WHERE me.name="My Name" AND
movie.title="Mystic River"
CREATE (me)-[:REVIEWED {rating:80, summary:"tragic character movie"}]->(movie);
另一种写法:
MATCH (me:Person {name:"My Name"}),
(movie:Movie {title:"Mystic River"})
CREATE (me)-[:REVIEWED {rating:80, summary:"tragic character movie"}]->(movie);
4merge:若不存在这个边,则增加这条关系:
MATCH (clint:Person),(mystic:Movie)
WHERE clint.name="Clint Eastwood" AND mystic.title="Mystic River"
MERGE (clint)-[:DIRECTED]->(mystic)
RETURN clint, mystic;
复杂一点的例子:
增加认识 knows关系:一部电影的演员与演员,演员或导演,或导演与导演之间可能认识:
MATCH (a)-[:ACTED_IN|:DIRECTED]->()<-[:ACTED_IN|:DIRECTED]-(b)
WHERE NOT (a)-[:KNOWS]-(b)
MERGE (a)-[:KNOWS]->(b);
5 删除节点和边
MATCH (me:Person {name:"My Name"})
OPTIONAL MATCH (me)-[r]-()
DELETE me,r;
6 长度等级输出
MATCH (node1)-[*]-(node2)
eg。MATCH (node1)-[*1..3]-(node2) : 三个长度等级的输出
7 shortestPath函数返回路径长度短的path
MATCH p=shortestPath( (node1)-[*]-(node2) )
RETURN length(p), nodes(p)