码迷,mamicode.com
首页 > 数据库 > 详细

Neo4j:图数据库GraphDB(三)

时间:2016-05-28 14:11:02      阅读:387      评论:0      收藏:0      [点我收藏+]

标签:

 

本片继续前几篇介绍图数据库的创建,有疑问可以我的看看前两篇文章:http://www.cnblogs.com/rongyux/p/5537206.html

 

四 图数据库的创建

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)

 

Neo4j:图数据库GraphDB(三)

标签:

原文地址:http://www.cnblogs.com/rongyux/p/5537260.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!