Neo4j AuraDB
The free remote graph database Neo4j officially provided by
Neo4j version 4
Region Belgium (europe-west1), GCP
Nodes 0 / 50000 (0%)
Relationships 0 / 175000 (0%)
The movie case commands are more abundant, for those who want to learn more Partners can register one.
View all databases**
SHOW DATABASES
Create a new database
CREATE DATABASE (database name is not case sensitive)
use this database
use (database name)
Verify database is empty
CALL db.schema.visualization()
View all data in the database
MATCH (n) RETURN n
Delete all data in the database
MATCH (n) DETACH DELETE n
Check for phrases
Format
The RETURN clause comes with three sub-clauses:
SKIP skip a few lines
MATCH (n)
RETURN n.name
ORDER BY n.name
SKIP 1 + toInteger(3*rand())
Skip the first line plus a random 0, 1 or 2. So randomly skip 1, 2 or 3 rows.
LIMIT limits a few lines
MATCH (n)
RETURN n.name
ORDER BY n.name
LIMIT 1 + toInteger(3 * rand())
Limit 1 row plus random 0, 1 or 2. So the random limit is 1, 2 or 3 rows.
ORDER BY ascending order (by the value of the specified item) + DESC (descending order)
MATCH (n)
RETURN n.name, n.age
ORDER BY n.age, n.name
This will return the nodes, sorted first by age, then by name.
MATCH (n)
RETURN n.name, n.age, n.length
ORDER BY keys(n)
Return nodes, sorted by their attributes.
MATCH (n)
RETURN n.name, n.age
ORDER BY n.name DESC
This example returns the nodes, sorted by their names in reverse order.
MATCH (n)
RETURN n.length, n.name, n.age
ORDER BY n.length
null
When sorting a result set, the results are always sorted at the end of the result set in ascending order and at the front when sorted in descending order.
String literals can contain the following escape sequences:
escape sequence | Features |
---|---|
\t | Label |
\b | backspace |
\n | new team |
\r | Enter |
\f | Page change |
’ | apostrophe |
" | Double quotes |
\ | backslash |
\uxxxx | Unicode UTF-16 code point (must be followed by 4 hexadecimal digits\u) |
Comment
Comments begin with a double slash //
and continue to the end of the line. Comments are not executed, they are meant to be read by humans.
MATCH (n) RETURN n //This is an end of line comment
MATCH (n)
//This is a whole line comment
RETURN n
relation
If we want to describe some data such that relationships can have any one of a set of types, then they can all be listed in the schema, separating them with the pipe symbol
(a)-[r:TYPE1|TYPE2]->(b)
Note that this form of pattern can only be used to describe existing data (that is, when using MATCH
the pattern with or as an expression). It does not work with CREATE
or MERGE
since it is not possible to create a relationship with multiple types.
(a)-[*3..5]->(b)
The minimum length is 3 and the maximum length is 5. It describes a graph consisting of 4 nodes and 3 relations, 5 nodes and 4 relations, or 6 nodes and 5 relations, all connected together in a path
Movie case
Match (m:Movie) where m.released > 2000 RETURN m limit 5
Result : The query returns all movies released after 2000, limiting the results to 5 items.
Match (m:Movie) where m.released > 2005 RETURN count(m)
Result : The query returns the number of movies released after 2005. (Tip: You can use this count(m)
function to return the quantity)
MATCH (p:Person)-[d:DIRECTED]-(m:Movie) where m.released > 2010 RETURN p,d,m
Result : The above query will return all people who directed movies released after 2010.
MATCH (p:Person) RETURN p limit 20
Result : Only Person
nodes will be returned (limited to 20 items)
MATCH (m:Movie) return m.title, m.released
Result : Returns the specific properties of the node
Match (p:Person {name: 'Tom Hanks'}) RETURN p
等价于
MATCH (p:Person) where p.name = "Tom Hanks" RETURN p
Result : The results of both statements are the same
MERGE (p:Person {name: 'John Doe'})
ON MATCH SET p.lastLoggedInAt = timestamp()
ON CREATE SET p.createdAt = timestamp()
Return p
Result : If the Person node does not exist, the above statement will create it. If the node already exists, then it will set the property lastLoggedInAt
to the current timestamp. If the node does not exist and is newly created, then it will createdAt
set this property to the current timestamp.
MATCH (p:Person), (m:Movie)
WHERE p.name = "Tom Hanks" and m.title = "Cloud Atlas"
CREATE (p)-[w:WATCHED]->(m)
RETURN type(w)
等价于
MATCH (p:Person{name:"Tom Hanks"}), (m:Movie{title:"Cloud Atlas"})
CREATE (p)-[w:WATCHED]->(m)
RETURN type(w)
Result : Creates a relationship WATCHED
between an existing node and node , and returns the relationship type (ie )Person
Movie
WATCHED
MATCH (m:Movie {title: 'Cloud Atlas'})<-[d:DIRECTED]-(p:Person) return p.name
Results : Find out who directed the Cloud Atlas movie
MATCH (tom:Person {name: "Tom Hanks"})-[:ACTED_IN]->(:Movie)<-[:ACTED_IN]-(p:Person) return p.name
Result : Find everyone who has worked with Tom Hanks in any movie
MATCH (p:Person)-[relatedTo]-(m:Movie {title: "Cloud Atlas"}) return p.name, type(relatedTo)
Result : Find everyone related to the movie Cloud Atlas in any way
MATCH (p:Person {name: 'Kevin Bacon'})-[*1..3]-(hollywood) return DISTINCT p, hollywood
Result : Find movies and actors with a distance of 3 from Kevin Bacon
Neo4j Cypher Manual
Query friends of john’s friends
MATCH (john {name: 'John'})-[:FRIEND]->()-[:FRIEND]->(fof)
RETURN john.name, fof.name
等价于
MATCH
(user:User {name: 'Adam'})-[r1:FRIEND]-(friend),
(friend)-[r2:FRIEND]-(friend_of_a_friend)
RETURN friend_of_a_friend.name AS fofName
Get a list of usernames and from that list find all nodes with names that match their friends and return only those followed users who have a 'name' attribute starting with 'S'
MATCH (user)-[:FRIEND]->(follower)
WHERE user.name IN ['Joe', 'John', 'Sara', 'Maria', 'Steve'] AND follower.name =~ 'S.*'
RETURN user.name, follower.name
When you want to use aggregated data for filtering, you have to chain two read query parts together, the first doing the aggregation and the second filtering the results from the first.
WITH
Like an event horizon - it is the dividing line between a plan and the completed execution of that plan
MATCH (n {name: 'John'})-[:FRIEND]-(friend)
WITH n, count(friend) AS friendsCount
WHERE friendsCount > 3
RETURN n, friendsCount
Find all paths with only 5 relationships and don't care about relationship direction
MATCH p = ()-[*5]-()
RETURN nodes(p)
multiple matches
MATCH (user:User {name: 'Adam'})-[r1:FRIEND]-(friend)
MATCH (friend)-[r2:FRIEND]-(friend_of_a_friend)
RETURN friend_of_a_friend.name AS fofName
calculate
syntax:
CASE test
WHEN value THEN result
[WHEN ...]
[ELSE default]
END
Name | describe |
---|---|
test |
a valid expression. |
value |
An expression whose result is compared to test . |
result |
If value matched, this is the expression returned as output test . |
default |
default Returns if no match is found . |
Example:
MATCH (n)
RETURN
CASE n.eyes
WHEN 'blue' THEN 1
WHEN 'brown' THEN 2
ELSE 3
END AS result
MATCH (n)
RETURN
CASE
WHEN n.eyes = 'blue' THEN 1
WHEN n.age < 40 THEN 2
ELSE 3
END AS result
MATCH (n)
RETURN n.name,
CASE
WHEN n.age IS NULL THEN -1
ELSE n.age - 10
END AS age_10_years_ago
update set
Query returns newly changed nodes
MATCH (n {name: 'Andy'})
SET n.surname = 'Taylor'
RETURN n.name, n.surname
SET
Can be used to update an attribute on a node or relationship that age
has been converted to a string '36'
.
MATCH (n {name: 'Andy'})
SET n.age = toString(n.age)
RETURN n.name, n.age
Although REMOVE
typically used to delete attributes, SET
it is sometimes convenient to use this command
MATCH (n {name: 'Andy'})
SET n.name = null
RETURN n.name, n.age
SET
Can be used to copy all attributes from one node or relationship to another
MATCH
(at {name: 'Andy'}),
(pn {name: 'Peter'})
SET at = pn
RETURN at.name, at.age, at.hungry, pn.name, pn.age
Replace attribute is empty
MATCH (p {name: 'Peter'})
SET p = {}
RETURN p.name, p.age
Update while keeping the original properties unchanged
MATCH (p {name: 'Peter'})
SET p += {age: 38, hungry: true, position: 'Entrepreneur'}
RETURN p.name, p.age, p.hungry, p.position
That won't work
MATCH (p {name: 'Peter'})
SET p += {}
RETURN p.name, p.age
Add properties
MATCH (n {name: 'Andy'})
SET n.position = 'Developer', n.surname = 'Taylor'
RETURN n
shortest path search algorithm
MATCH (KevinB:Person {name: 'Kevin Bacon'} ),
(Al:Person {name: 'Al Pacino'}),
p = shortestPath((KevinB)-[:ACTED_IN*]-(Al))
WHERE all(r IN relationships(p) WHERE exists(r.role))
RETURN p
This query can be evaluated using a fast algorithm - no predicates require looking at the entire path before evaluating
For more statements, refer to https://neo4j.com/docs/cypher-manual/4.0/
load_csv
Backup address (you can create it yourself if there is no import)
dbms.directories.import=/var/lib/neo4j/import
Graph.db is mentioned in many articles, but I can't find it no matter how I look for it.
Enter the conf file under neo4j, open neo4j.conf, and add a line dbms.active_database=graph.db under #dbms.default_database=neo4j.
Delete database method (delete all)
All files under /var/lib/neo4j/ect/neo4j/neo4j.config
file