forked from awesomecosmos/CS623-Database-Management
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cs623_new_trends_code_demo.txt
51 lines (32 loc) · 1.86 KB
/
cs623_new_trends_code_demo.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Querying a database in Neo4j
We have two 'tables' (called 'Nodes' in Neo4j) of data: Movie, and Person.
Nodes are connected to each other through Relationships.
In this demonstration, we show some Cypher queries which can be used to query a database in Neo4j.
# finding all movies released after 2000
MATCH (m:Movie) WHERE m.released > 2000 RETURN m
# obtaining count of all movies released in or after 2005
Match (m:Movie) where m.released >= 2005 return count(m)
# finding all 'Person' entities related to 'Movie' entities via the relationship 'Directed'
# i.e. finding all people who directed a movie after 2005
MATCH (p:Person)-[d:DIRECTED]-(m:Movie) where m.released > 2005 RETURN p,d,m
# returning all instances of 'Person' and their names and birth dates
MATCH(p:Person) RETURN p.name,p.born
# creating a new Person instance
Create (p:Person {name: 'John Doe'}) RETURN p
# finding a specific instance of Movie
MATCH (m:Movie) WHERE m.title = "Cloud Atlas" RETURN m
# finding all movies between the years 2000 and 2005 inclusive
MATCH (m:Movie) WHERE m.released >= 2000 AND m.released <=2005 RETURN m
# if 'John Doe' exists, then log current timestamp, otherwise create instance of 'John Doe' and then log current timestamp
MERGE (p:Person {name: 'John Doe'})
ON MATCH SET p.lastLoggedInAt = timestamp()
ON CREATE SET p.createdAt = timestamp()
Return p
# obtaining all Person-ActedIn-Movie relations
MATCH (p:Person)-[r:ACTED_IN]->(m:Movie) RETURN p,r,m
# obtaining all Directors of 'Cloud Atlas'
MATCH (m:Movie {title: 'Cloud Atlas'})<-[d:DIRECTED]-(p:Person) return p.name
# obtaining all Person relations related to the movie title 'Cloud Atlas'
MATCH (p:Person)-[relatedTo]-(m:Movie {title: "Cloud Atlas"}) return p.name, type(relatedTo)
# obtaining all 3rd-degree connections to 'Tom Hanks'
MATCH (p:Person {name: 'Tom Hanks'})-[*1..3]-(hollywood) return DISTINCT p, hollywood