当前位置:网站首页>Cypher syntax of neo4j graph database
Cypher syntax of neo4j graph database
2022-07-05 12:25:00 【Nat_ Jst】
CREATE (myroot:Entity {modelType:'target', name:"myroot", tableType:'table'})
CREATE (myroot_myjh:attribute {name:'myjh'})
CREATE (myroot_myjd:attribute {name:'myjd'})
CREATE (myroot_myjh)-[:att]->(myroot),// This statement cannot establish the relationship between the above two variables as imagined , The variables of the above statement cannot be passed to the following
Repeated execution will repeatedly create nodes
match(n) return n; // Query all nodes and edges
match(n:Entity) return n;// Query all nodes
match(n) where n.name IS NULL return n
match(n) where n.name IS NULL delete n // You need to delete the relationship first
MATCH ()-[r:attribute]-() return r;
MATCH ()-[r:attribute]-() delete r;
match(n) where n.name is null return n;
match (p)-[r]-(c) where c.name is null return p,r,c
match (p)-[r]-(c) where c.name is null
delete r,c
match(a:Entity) where a.name="myroot"
match(b:attribute) where b.name='myjd'
CREATE (a)-[:att]->(b)
match(a:Entity) where a.name="myroot"
match(b:attribute) where b.name='myjh'
CREATE (a)-[:att]->(b)
MATCH ()-[r:att]-() delete r;
// The following report is wrong
match(a:Entity) where a.name="myroot"
match(b:attribute) where b.name='myjd'
CREATE (a)-[:att]->(b)
match(c:attribute) where c.name='myjh'
CREATE (a)-[:att]->(c)
error message :
WITH is required between CREATE and MATCH (line 4, column 1 (offset: 100))
"match(c:attribute) where c.name='myjh'"
^
// The following is correct
match(a:Entity) where a.name="myroot"
match(b:attribute) where b.name='myjd'
match(c:attribute) where c.name='myjh'
CREATE (a)-[:att]->(b)
CREATE (a)-[:att]->(c)
or
match(a:Entity) where a.name="myroot"
match(b:attribute) where b.name='myjd'
match(c:attribute) where c.name='myjh'
CREATE (a)-[:att]->(b),
(a)-[:att]->(c)
repeat , Will repeatedly establish a relationship
//UNWIND、DISTINCT、collect
UNWIND [1, 2, 3, null] AS x
RETURN x, 'val' AS y
WITH [1, 1, 2, 2] AS coll
UNWIND coll AS x
WITH DISTINCT x
RETURN collect(x) AS setOfVals
WITH [[1, 2], [3, 4], 5] AS nested
UNWIND nested AS x RETURN x
WITH [[1, 2], [3, 4], 5] AS nested
UNWIND nested AS x
UNWIND x AS y
RETURN y
WITH [] AS list
UNWIND
CASE
WHEN list = []
THEN [null]
ELSE list
END AS emptylist
RETURN emptylist
------------------------
Neo4j Query statement summary
1. How to find a node x,x Connect two different nodes at the same time in a certain relationship a and b
match (a)-[r:relation]->(x)<-[r:relation]-(b) return x 2. How to find nodes a and b The shortest path between (1)match p=shortestpath((a)-[r:relation]-(b)) return nodes(p) (2)match(n:na{name:’###’}),(m:nb{name:’###’})with n,m match p=shortestpath((n)-[r*…]-(m)) return p; 3. How to find nodes a and b The shortest path connected by a certain relationship p=shortestpath((a)-[r:relationname]->(b)) return nodes(p)
4. Find the unique node label that appears in the database
match n return distinct labels(n)
5. Find the unique relationship type that appears in the database
match n-[r]-() return distinct type(r)
6. Find the unique node label and unique relationship type in the database
match n-[r]-() return distinct labels(n),type(r)
7. Find nothing to do with ( Or some kind of relationship ) Connected nodes
start n = node() match n-[r:relationname]-() where r is null return n
8. Find a node with specific attributes
start n=node() match n where has (n.someproperty) return n
9. Find all nodes connected to a relationship
start n= node() match n-[r:relationshipname]-() return distinct n
10. Find nodes and their relationships , And display in descending order of the number of relationships
start n=node() match n-[r]-() return n,count(r) as rel_count order by rel_count desc
11. Return the number of all nodes in the graph
start n = node() match n return count(n)
12.(1) Delete the relationship in the diagram :start n=node(*) match n-[r]-() delete r
(2) Delete nodes in the graph :start n =node(*) match n delete n
(3) Delete everything in the picture :match (n) detach delete n
13. Query a node with a specific attribute value under a certain type of node
match (n:person)where n.name=”alice” return n
14.with
Cypher Medium With Keyword can take the result of the previous query as the condition of the next query , This is a big help in my work, haha . Here are two chestnuts .
(1)match(p:node_se)-[re: Reasoning conditions ]->(q:node_se) where p.name=‘FEV1%pred’and p.value=’<30%’ WITH p,re,q match (q:node_se) <-[re2: Reasoning conditions ]- (c:node_se)return p, re,q,re2,c (2)match(p:node_patient)-[re: Personal situation ]->(q:node_se) where p.name=‘qwe’ WITH p,re,q match (q:node_se) -[re2: Recommended solution ]-> (c:node_se) where q.name=‘first’ WITH p, re,q,re2,c match (c:node_se)-[re3: Details of the plan ]->(d:drugs) return p, re,q,re2,c,re3,d
15. Query the of a node that meets the conditions id
match(p) where p.name = ‘***’ and p.value = ‘***’ return id(p)
16. Directly connect relational nodes for multi-level queries
match(na:bank{id:‘001’})-[re1]->(nb:company)-[re2]->(nc:people) return na,re1,nb,re2,nc
17. You can assign the query results to variables , Then return
match data=(na:bank{id:‘001’})-[re1]->(nb:company)-[re2]->(nc:company) return data
18. Variable length path retrieval
The representation of variable length path is :[*N…M],N and M Represents the minimum and maximum values of the path length .
(a)-[ *2]->(b): Indicates that the path length is 2, The starting node is a, The termination node is b;
(a)-[ *3…5]->(b): The minimum value indicating the path length is 3, The maximum is 5, The starting node is a, The termination node is b;
(a)-[ *…5]->(b): The maximum path length is 5, The starting node is a, The termination node is b;
(a)-[ *3…]->(b): The minimum value indicating the path length is 3, The starting node is a, The termination node is b;
(a)-[ *]->(b): Means unlimited path length , The starting node is a, The termination node is b;
19.Cypher De duplication of query results
Chestnut :match(p:node_se)-[re]->(q)where re.name <> ‘and’ return distinct(re.name)
( notes : In chestnuts <> by Cypher One of the operators in , Express ‘ It's not equal to ’)
20. Update the labels
Neo4j A node in can have multiple label, Return all nodes label:match (n) return labels(n)
Modify the label, You can add new label, Delete the old label
match (n:label_old) set n:label_new remove n:label_old
match(n:label_new) return labels(n)
21. Update the properties of the node
match(n:) set n.new_property = n.old_property remove n.old_proerty
Let's summarize these first , Feeling cypher It's interesting , In the project, I often think about how to write it, which feels like fun .
above .
————————————————
Copyright notice : This paper is about CSDN Blogger 「 Cherry little fatty classmate 」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/weixin_40771521/article/details/95936491
//0: Ready to delete everything , It is required to delete the relationship first , Delete the node again , Related nodes cannot be deleted
match ()-[r]-() delete r
match (n) delete n
1. Create nodes
CREATE (st101:Student {name:' Zhang San ', no:'101'})
CREATE (st102:Student {name:' Li Si ', no:'102'})
CREATE (st103:Student {name:' Wang Wu ', no:'103'})
CREATE
(c01:Course {name:' Big data storage and management technology ',credit:4}),
(c02:Course {name:' Big data application technology ',credit:4}),
(c03:Course {name:' database ',credit:4})
CREATE
(t01:Teacher {name:' Teacher Wen ', title:' professor '}),
(t02:Teacher {name:' Miss Lu ', title:' associate professor '}),
(t03:Teacher {name:' Miss Bao ', title:' professor '})
CREATE
(st101)-[: Elective ]->(c01),
(st102)-[: Elective ]->(c01),
(st102)-[: Elective ]->(c02),
(st103)-[: Elective ]->(c01),
(st103)-[: Elective ]->(c02),
(st103)-[: Elective ]->(c03)
CREATE
(t01)-[: Lecture ]->(c01),
(t02)-[: Lecture ]->(c02),
(t03)-[: Lecture ]->(c03),
(t04)-[: Lecture ]->(c04)
CREATE (:Teacher {name:' Miss Zhao ', The title :' professor '}) // Chinese can be used as attribute , You can also use Chinese as the nodal name
CREATE (:Province {name:' heilongjiang ',abbreviation:' black '})
CREATE (:Province {name:' hebei ',abbreviation:' Ji '})
2.-- Here are three examples of creating relationships , Please execute it separately
MATCH (a:Student), (b:Province) WHERE a.name = ' Zhang San ' AND b.name = ' heilongjiang '
CREATE (a)-[r: come from ]->(b)
MATCH (c:Student {name:' Li Si '}), (d:Province {name:' heilongjiang '})
CREATE (c)-[r: come from ]->(d)
MATCH (c:Student {name:' Wang Wu '}), (d:Province {name:' hebei '})
CREATE (c)-[r: come from ]->(d)
// A king five will be created separately , A Hebei , A relationship
CREATE (:Student {name:' Wang Wu '})-[r: come from ]->(:Province {name:' hebei '})
// The above results are not what we want , Delete the
match (s:Student {name:' Wang Wu '})-[r: come from ]->(p:Province {name:' hebei '})
delete r,s,p
3. modify
MATCH (s:Student{name:' Li Si '})-[r1]-(b:Province) // Change Li Si's native place to Hebei
CREATE (s)-[r2: come from ]->(:Province{name:' hebei '}) // So let's create a new one ,
DELETE r1 // Delete the original
4. Various inquiries
4.1 Query all nodes and edges :
match (n) return (n)
match all=(p:Province)-[r]-(s:Student) return all // Inquire the native place of each student
MATCH all=(s:Student{name:' Li Si '})-[r]-(b:Province) return all // Inquire the native place of a student
match (t:Teacher{name:' Teacher Wen '}), (p:Province{name:' hebei '}), path=shortestpath((t)-[*..3]-(p))
return path // Mr. Wen taught Hebei students
match (t:Teacher{name:' Teacher Wen '})-[]-(s:Student)-[]-(p:Province{name:' hebei '})
return s,t,p,(s)-[]-(t),(s)-[]-(p) // Mr. Wen taught Hebei students
CREATE (: Province {name:' heilongjiang ',abbreviation:' black '}) // It can be labeled in Chinese , But you can't query
MATCH (p: Province ) delete p
----
//neo4j How to directly return the nodes and relationships of the three layers around the node
match data=(t:Teacher{name:' Teacher Wen '})-[rel*1..3]-(b) return t,rel,b
match data=(t:Teacher{name:' Teacher Wen '})-[rel*1..3]-(b) return t,rel,b
match (t:Teacher)-[rel*1..1]-(b) where t.name=' Teacher Wen ' return t,rel,b
--->
This feature is deprecated and will be removed in future versions.
Binding relationships to a list in a variable length pattern is deprecated. (Binding a variable length relationship pattern to a variable ('rel') is deprecated and will be unsupported in a future version. The recommended way is to bind the whole path to a variable, then extract the relationships:
MATCH p = (...)-[...]-(...)
WITH *, relationships(p) AS rel)
// Teacher Wen taught the students in Heilongjiang
match p=(t:Teacher)-[*]-(b:Province)
where t.name=' Teacher Wen ' and b.name=' heilongjiang '
return p
---------------------
MATCH (a:Teacher), (b:Course)
WHERE a.name = ' Teacher Wen ' AND b.name = ' Big data storage and management technology '
CREATE (a)-[r: Lecture ]->(b)
match (p)-[r]-(b) where p.name=' Teacher Wen ' // Query all nodes and relationships related to teacher Wen
return p,r,b
----------------------------
match(n) return n; // Query all nodes and edges
match(n:Teacher) return n;// Check all tags for Teacher The node of
match (p:Teacher) where p.name=' Teacher Wen ' return p;// Conditions of the query
match (p:Student) where p.name in [' Wang Wu ',' Li Si '] return p;
match (t)-[r: Lecture ]->(c) return r
MATCH (a:Person), (b:Person)
WHERE a.name = 'A' AND b.name = 'B'
CREATE (a)-[r:RELTYPE]->(b)
5. Find the unique relationship type that appears in the database
match ()-[r]-() return distinct type(r)
----------------------------
// Update the label name , Property name
occasionally Find node's label How to change the wrong name ?!
A node can have multiple nodes label Of , its labels It's a list .
View the label It can be used labels(n) command .
therefore , You want to modify the node label , You can add new label , Delete the old label
match (n:CAR) set n:NEW remove n:CAR
match(n:NEW) RETURN labels(n)
Empathy , You can modify the node property
match(n:CAR) SET n.new_property = n.old_property remove n.old_proerty
This also adds a new attribute to the node , meanwhile , Assign the value of the attribute to the new attribute . Then delete the old attributes
// Add an attribute to the node , Direct assignment
match (p:Teacher) where p.name=' Teacher Wen '
set p.birthDay='1967.09.13'
// Show all attributes of all teachers
match (p:Teacher) return properties(p)
// It can be seen that not all Teacher All have the same properties
{
"birthDay": "1967.09.13",
"name": " Teacher Wen ",
"title": " professor "
}
{
"name": " Miss Lu ",
"title": " associate professor "
}
// Query all tag names ------------
Method 1:match (n) return distinct labels(n) as Tag name
Tag name
["Student"]
["Course"]
["Teacher"]
no need disticnt There will be repetition
match (n) return labels(n) as Tag name ,count(*) as The number of instances
Tag name The number of instances
["Student"] 3
["Course"] 6
["Teacher"] 4
Method 2: Call function
call db.labels
"Student"
"Course"
"Teacher"
-------------------------------------
match (p:Student) where p.name in [' Wang Wu ',' Li Si ']
match (c:Course) where c.name =' Big data application technology '
CREATE
(p)-[: Elective ]->(c)
match (p:Student) where p.name in [' Zhang San ',' Li Si ']
match (c:Course) where c.name =' Big data storage and management technology '
CREATE
(p)-[: Elective ]->(c)
match (p:Student)-[r: Elective ]->(c:Course) return p,r,c // Check the course selection of all students
match (p)-[r: Elective ]->(c) return p,r,c // Show the course selection of all students
match (t:Teacher)-[r: Lecture ]->(c:Course) return t,r,c // Show all lectures
match (t)-[r: Lecture ]->(c) return t,r,c // Show all lectures
match (p:Student) where p.name in [' Zhang San ',' Li Si ']
return (p)-[: Elective ]->()
match (p)-[r: Elective ]->(c)
match (t)-[r: Lecture ]->(c)
create (p)-[: Listen to the teacher ]->(t) // You need to create a listening relationship before you can query
match (p)-[: Elective ]->(c)<-[: Lecture ]-(t) return p,(p)-[: Listen to the teacher ]->(t),t // Trying to return a relationship without a trailer definition , But there was no result
5. Find the unique relationship type that appears in the database
match ()-[r]-() return distinct type(r)
边栏推荐
- Handwriting blocking queue: condition + lock
- Simple production of wechat applet cloud development authorization login
- How can beginners learn flutter efficiently?
- Vscode shortcut key
- What is digital existence? Digital transformation starts with digital existence
- JS for循环 循环次数异常
- ACID事务理论
- C language structure is initialized as a function parameter
- Four operations and derivative operations of MATLAB polynomials
- Recyclerview paging slide
猜你喜欢
Linux Installation and deployment lamp (apache+mysql+php)
7月华清学习-1
ABAP table lookup program
Reinforcement learning - learning notes 3 | strategic learning
Matlab struct function (structure array)
Why learn harmonyos and how to get started quickly?
Master the new features of fluent 2.10
Get data from the database when using JMeter for database assertion
HiEngine:可媲美本地的云原生内存数据库引擎
Embedded software architecture design - message interaction
随机推荐
Redis highly available sentinel mechanism
PIP command reports an error pip is configured with locations that requires tls/ssl problems
2022年国内云管平台厂商哪家好?为什么?
Time tools
Which domestic cloud management platform manufacturer is good in 2022? Why?
How can beginners learn flutter efficiently?
A guide to threaded and asynchronous UI development in the "quick start fluent Development Series tutorials"
[superhard core] is the core technology of redis
Seven ways to achieve vertical centering
MySQL index - extended data
Multi table operation - sub query
Conversion du format de données GPS [facile à comprendre]
Differences between IPv6 and IPv4 three departments including the office of network information technology promote IPv6 scale deployment
Understand redis persistence mechanism in one article
Array cyclic shift problem
Learn JVM garbage collection 02 - a brief introduction to the reference and recycling method area
July Huaqing learning-1
信息服务器怎么恢复,服务器数据恢复怎么弄[通俗易懂]
Solve the problem of cache and database double write data consistency
Design of music box based on assembly language