绑定完请刷新页面
取消
刷新

分享好友

×
取消 复制
Neo4j:图数据库GraphDB(三)创建删除及操作
2022-04-18 11:17:56

四 图数据库的创建

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)

分享好友

分享这个小栈给你的朋友们,一起进步吧。

GraphDB
创建时间:2022-04-18 10:29:03
GraphDB
展开
订阅须知

• 所有用户可根据关注领域订阅专区或所有专区

• 付费订阅:虚拟交易,一经交易不退款;若特殊情况,可3日内客服咨询

• 专区发布评论属默认订阅所评论专区(除付费小栈外)

技术专家

查看更多
  • LCR_
    专家
戳我,来吐槽~