Store any kind of data using the following graph concepts:
- Node: Graph data records
- Relationship: Connect nodes (has direction and a type)
- Property: Stores data in key-value pair in nodes and relationships
- Label: Groups nodes and relationships (optional)
MATCH (n:Person)
WHERE n.name = "Andre"
RETURN (n);
- MATCH clause to specify a pattern of nodes and relationships
- (n:Person) a single node pattern with label 'Person' which will assign matches to the variable
n
- WHERE clause to constrain the results
- n.name = "Andre" compares name property to the value "Andre"
- RETURN clause used to request particular results
Gets gets the id<5> and id<0> nodes and creates a :KNOWS
relationship between them
MATCH (n:Person)-[:KNOWS]-(friends)
WHERE n.name = "Andre"
RETURN n, friends
- MATCH clause to describe the pattern from known Nodes to found Nodes
- (n) starts the pattern with a Person (qualified by WHERE)
- -[:KNOWS]- matches "KNOWS" relationships (in either direction)
- (friends) will be bound to Andre's friends
MATCH (n:Person)
RETURN n;
or
MATCH (n)
WHERE n:Person
RETURN n;
:Car
OR :Person
labels
MATCH (n)
WHERE n:Person OR n:Car
RETURN n;
:Car
AND :Person
labels
MATCH (n)
WHERE n:Person:Car
RETURN n;
MATCH (a:Person)
WHERE a.from = "Sweden"
RETURN a;
match (n:MyNode)-[r]->(m)
return n, r, m;
match p=(a)-[:TO*]-(c)
where a.Name='H' and c.Name='P'
return p limit 1;
match p=(a)-[:TO*]-(c) where a.Name='H' and c.Name='P'
return p order by length(p) asc limit 1;
match p=(a)-[:TO*]-(c)
where a.Name='H' and c.Name='P'
return length(p) limit 1;
match p=shortestPath((a)-[:TO*]-(c))
where a.Name='A' and c.Name='P'
return p, length(p) limit 1;
MATCH p = allShortestPaths((source)-[r:TO*]-(destination))
WHERE source.Name='A' AND destination.Name = 'P'
RETURN EXTRACT(n IN NODES(p)| n.Name) AS Paths;
MATCH p = allShortestPaths((source)-[r:TO*]->(destination))
WHERE source.Name='A' AND destination.Name = 'P' AND LENGTH(NODES(p)) > 5
RETURN EXTRACT(n IN NODES(p)| n.Name) AS Paths,length(p);
match (n:MyNode), (m:MyNode)
where n <> m
with n, m
match p=shortestPath((n)-[*]->(m))
return n.Name, m.Name, length(p)
order by length(p) desc limit 1;
match p=(a)-[:TO*]-(c)
where a.Name='H' and c.Name='P'
return extract(n in nodes(p)|n.Name) as Nodes, length(p) as pathLength,
reduce(s=0, e in relationships(p)| s + toInt(e.dist)) as pathDist limit 1;
MATCH (from: MyNode {Name:'A'}), (to: MyNode {Name:'P'}),
path = shortestPath((from)-[:TO*]->(to))
WITH REDUCE(dist = 0, rel in rels(path) | dist + toInt(rel.dist)) AS distance, path
RETURN path, distance
MATCH (from: MyNode {Name:'A'}), (to: MyNode),
path = shortestPath((from)-[:TO*]->(to))
WITH REDUCE(dist = 0, rel in rels(path) | dist + toInt(rel.dist)) AS distance, path, from, to
RETURN from, to, path, distance order by distance desc
match (n)-[r:TO]->(m)
where n.Name <> 'D' and m.Name <> 'D'
return n, r, m
match p=shortestPath((a {Name: 'A'})-[:TO*]-(b {Name: 'P'}))
where not('D' in (extract(n in nodes(p)|n.Name)))
return p, length(p)
match (d {Name:'D'})-[:TO]-(b)
with collect(distinct b.Name) as neighbors
match (n)-[r:TO]->(m)
where
not (n.Name in (neighbors+'D'))
and
not (m.Name in (neighbors+'D'))
return n, r, m
;
match (d {Name:'D'})-[:TO]-(b)-[:TO]->(leaf)
where not((leaf)-->())
return (leaf)
;
match (d {Name:'D'})-[:TO]-(b)<-[:TO]-(root)
where not((root)<--())
return (root)
match (a {Name: 'F'})-[:TO*..2]-(b)
with collect(distinct b.Name) as MyList
match (n)-[r:TO]->(m)
where not(n.Name in MyList) and not (m.Name in MyList)
return distinct n, r, m