1.节点创建
# 创建一个Label为Person的节点,且该阶段有属性 name,from,klout
CREATE (ee:Person { name: "Emil", from: "Sweden", klout: 99 })
2.查询数据
# 从 Label 为 Person 的数据中,找到一条数据,过滤条件 name = "Emil"
MATCH (ee:Person) WHERE ee.name = "Emil" RETURN ee;
3.创建更多数据
# 首先,从 Label 为 Person 的数据中过滤出name = "Emil"的数据
# 然后,创建多条Label为Person的数据
# 接着,创建每条节点数据间的关系 KNOWS
MATCH (ee:Person) WHERE ee.name = "Emil"
CREATE (js:Person { name: "Johan", from: "Sweden", learn: "surfing" }),
(ir:Person { name: "Ian", from: "England", title: "author" }),
(rvb:Person { name: "Rik", from: "Belgium", pet: "Orval" }),
(ally:Person { name: "Allison", from: "California", hobby: "surfing" }),
(ee)-[:KNOWS {since: 2001}]->(js),(ee)-[:KNOWS {rating: 5}]->(ir),
(js)-[:KNOWS]->(ir),(js)-[:KNOWS]->(rvb),
(ir)-[:KNOWS]->(js),(ir)-[:KNOWS]->(ally),
(rvb)-[:KNOWS]->(ally)
4.通过关系找数据
#查找所有与name = "Emil"这条数据有 KNOWS关系的数据a
MATCH (ee:Person)-[:KNOWS]-(a) WHERE ee.name = "Emil" RETURN ee, a
5.通过节点属性找数据
# 首先,查找一条数据 Label为Person,name = "Johan"
# 然后,查询与它间隔一个节点的数据,且之间的关系需是 KNOWS,并且忽视关系方向
# 最后,需要查找的节点数据的属性 hobby = "surfing"
MATCH (js:Person)-[:KNOWS]-()-[:KNOWS]-(surfer)
WHERE js.name = "Johan" AND surfer.hobby = "surfing"
RETURN DISTINCT surfer
6.间隔节点找数据
# 查找与数据name='Johan' 间隔一个节点的所有数据节点
match (a:Person)-[:KNOWS]-()-[:KNOWS]-(b) where a.name='Johan' return b
7.根据不同条件过滤数据
# 查找所有name: "Tom Hanks"的数据,任何Label
MATCH (tom {name: "Tom Hanks"}) RETURN tom
# 查找 title: "Cloud Atlas" 的所有数据
MATCH (cloudAtlas {title: "Cloud Atlas"}) RETURN cloudAtlas
# 查找所有 Label 为 Person 的数据,返回其name 属性,并限定10条
MATCH (people:Person) RETURN people.name LIMIT 10
# 匹配所有 Label 为 Person 的数据,过滤出 released >= 1990 AND released < 2000,并且返回字段 title
MATCH (nineties:Movie) WHERE nineties.released >= 1990 AND nineties.released < 2000 RETURN nineties.title
# 找寻 Tom Hanks 出演的所有电影
MATCH (tom:Person {name: "Tom Hanks"})-[:ACTED_IN]->(tomHanksMovies) RETURN tom,tomHanksMovies
# 找寻所有出演 Cloud Atlas 电影的演员的名字
MATCH (cloudAtlas {title: "Cloud Atlas"})<-[:DIRECTED]-(directors) RETURN directors.name
# 找寻与 Tom Hanks 一起合作过电影的演员名称
MATCH (tom:Person {name:"Tom Hanks"})-[:ACTED_IN]->(m)<-[:ACTED_IN]-(coActors) RETURN coActors.name
#找寻与电影Cloud Atlas有关系的所有人,返回人名,关系类型
MATCH (people:Person)-[relatedTo]-(:Movie {title: "Cloud Atlas"}) RETURN people.name, Type(relatedTo), relatedTo
# 找寻 Label 为 Person,名字为Kevin Bacon,所有1-4层的所有数据
MATCH (bacon:Person {name:"Kevin Bacon"})-[*1..4]-(hollywood)
RETURN DISTINCT hollywood
# 找寻 name:"Kevin Bacon"和name:"Kevin Bacon" 这两个人之间的最短路径
MATCH p=shortestPath(
(bacon:Person {name:"Kevin Bacon"})-[*]-(meg:Person {name:"Meg Ryan"})
)
RETURN p
# 找寻 Tom Hanks合作过的演员a,演员 a合作过的演员b,演员b中未与 Tom Hanks合作过的演员
MATCH (tom:Person {name:"Tom Hanks"})-[:ACTED_IN]->(m)<-[:ACTED_IN]-(coActors),
(coActors)-[:ACTED_IN]->(m2)<-[:ACTED_IN]-(cocoActors)
WHERE NOT (tom)-[:ACTED_IN]->()<-[:ACTED_IN]-(cocoActors) AND tom <> cocoActors
RETURN cocoActors.name AS Recommended, count(*) AS Strength ORDER BY Strength DESC
# 与Tom Hanks一同演过电影的演员a,与演员a一同演过电影的名为Tom Cruise
MATCH (tom:Person {name:"Tom Hanks"})-[:ACTED_IN]->(m)<-[:ACTED_IN]-(coActors),
(coActors)-[:ACTED_IN]->(m2)<-[:ACTED_IN]-(cruise:Person {name:"Tom Cruise"})
RETURN tom, m, coActors, m2, cruise
8.匹配所有数据
MATCH (n) RETURN n
9.删除所有数据
# 删除所有节点与关系的数据
MATCH (n) DETACH DELETE n
10.创建索引
#创建索引,主要为了提高查询性能
CREATE INDEX ON :Product(productID)
11.索引字段关联创建关系
#索引字段相当于外键,通过外键关联,创建有指向性的关系
MATCH (p:Product),(c:Category) WHERE p.categoryID = c.categoryID CREATE (p)-[:PART_OF]->(c)
12.返回数据可使用函数
#找寻 Label为Supplier/Category,关系为Product,方向为Supplier->Category的数据,返回companyName ,以及categoryName,并将categoryName转换为数组结构
MATCH (s:Supplier)-->(:Product)-->(c:Category) RETURN s.companyName as Company, collect(distinct c.categoryName) as Categories
网友评论