Neo4j - Read Clause

1. Match Clause

1.1 Get All Nodes Using Match

Using the MATCH clause of Neo4j you can retrieve all nodes in the Neo4j database.
eg.
Before proceeding with the example, create 3 nodes and 2 relationships as shown below.

CREATE (Dhoni:player {name: "MahendraSingh Dhoni", YOB: 1981, POB: "Ranchi"}) 
CREATE (Ind:Country {name: "India", result: "Winners"}) 
CREATE (CT2013:Tornament {name: "ICC Champions Trophy 2013"}) 
CREATE (Ind)-[r1:WINNERS_OF {NRR:0.938 ,pts:6}]->(CT2013) 

CREATE(Dhoni)-[r2:CAPTAIN_OF]->(Ind)  
CREATE (Dhawan:player{name: "shikar Dhawan", YOB: 1995, POB: "Delhi"}) 
CREATE (Jadeja:player {name: "Ravindra Jadeja", YOB: 1988, POB: "NavagamGhed"})  

CREATE (Dhawan)-[:TOP_SCORER_OF {Runs:363}]->(Ind) 
CREATE (Jadeja)-[:HIGHEST_WICKET_TAKER_OF {Wickets:12}]->(Ind)
Match Result

1.2 Getting All Nodes Under a Specific Label

MATCH (node:label) 
RETURN node 

eg
Following is a sample Cypher Query, which returns all the nodes in the database under the label player.

MATCH (n:player) 
RETURN n 

1.3 Match by Relationship

You can retrieve nodes based on relationship using the MATCH clause.

MATCH (node:label)<-[: Relationship]-(n) 
RETURN n 

eg

MATCH (Ind:Country {name: "India", result: "Winners"})<-[: TOP_SCORER_OF]-(n) 
RETURN n.name 
image.png

2. Option Match Clause

The OPTIONAL MATCH clause is used to search for the pattern described in it, while using nulls for missing parts of the pattern.
OPTIONAL MATCH is similar to the match clause, the only difference being it returns null as a result of the missing parts of the pattern.

MATCH (node:label {properties. . . . . . . . . . . . . .}) 
OPTIONAL MATCH (node)-->(x) 
RETURN x

eg.
Following is a sample Cypher Query which tries to retrieve the relations from the node ICCT2013. Since there are no such nodes, it returns null.

MATCH (a:Tornament {name: "ICC Champions Trophy 2013"}) 
OPTIONAL MATCH (a)-->(x) 
RETURN x 

Here you can observe that since there are no matches for the required pattern, Neo4j returned null.


Optional Match Result

3. Where Clause

Neo4j CQL has provided WHERE clause in CQL MATCH command to filter the results of a MATCH Query.

MATCH (label)  
WHERE label.country = "property" 
RETURN label 

eg.
Before proceeding with the example, create five nodes in the database as shown below.

CREATE(Dhawan:player{name:"shikar Dhawan", YOB: 1985, runs:363, country: "India"})
CREATE(Jonathan:player{name:"Jonathan Trott", YOB:1981, runs:229, country:"South Africa"})
CREATE(Sangakkara:player{name:"Kumar Sangakkara", YOB:1977, runs:222, 
   country:"Srilanka"})
CREATE(Rohit:player{name:"Rohit Sharma", YOB: 1987, runs:177, country:"India"})
CREATE(Virat:player{name:"Virat Kohli", YOB: 1988, runs:176, country:"India"})
CREATE(Ind:Country {name: "India", result: "Winners"})

Following is a sample Cypher Query which returns all the players (nodes) that belongs to the country India using WHERE clause.

MATCH (player)  
WHERE player.country = "India" 
RETURN player 
注意,这块的截图是点了table的,不是Graph

3.1 WHERE Clause with Multiple Conditions

MATCH (player)  
WHERE player.country = "India" AND player.runs >=175 
RETURN player 
image.png

3.2 Using Relationship with Where Clause

MATCH (n) 
WHERE (n)-[: TOP_SCORER_OF]->( {name: "India", result: "Winners"}) 
RETURN n 

Here you can observe that Neo4j returned the node, which has the relation TOP_SCORER_OF to the country with the node having the name India.


image.png

4. Count

4.1 Count

The count() function is used to count the number of rows.

MATCH (n { name: 'A' })-->(x) 
RETURN n, count(*) 

eg.

Match(n{name: "India", result: "Winners"})--(x)  
RETURN n, count(*) 

4.2 Group Count

The COUNT clause is also used to count the groups of relationship types.
Following is a sample Cypher Query which counts and returns the number of nodes participating in each relation.

Match(n{name: "India", result: "Winners"})-[r]-(x)  
RETURN type (r), count(*) 
image.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 夏雨滂沱,偶发雷鸣闪电,偌大陵王府那被岁月消耗去的,仅剩凡几的风华样貌,也被这突如其来的雷雨惊散,显现出那些破败感...
    蔷金阅读 3,068评论 6 0
  • 续章 当一切都雨过天晴之后,沈巍也就是黑袍使与他的弟弟夜尊同归于尽,灵魂消散。...
    死亡幽歌阅读 11,385评论 1 2
  • 雨后 一片水的平原 一片沉寂 千百种虫翅不再振响 在马齿苋 肿痛的土地上 水虱追逐着颤动的波 花瓣、润红、淡蓝 苦...
    凯风bule阅读 1,283评论 0 4
  • 我一早准备好了照机本想去古纤道拍一组照片,可外面下起了雨,我站在书房看着外面,雨水洒落在河面上翻起了一个个波纹,突...
    雲烽摄影阅读 1,643评论 0 2

友情链接更多精彩内容