# Neo4j
A [[graph-db]].
## Concepts
- Node and relationships
- Both can have properties
- Uses Cypher query language
## Usage
```cypher
CREATE (p:Person { name: "Micheal Cera", born: 1988 });
MATCH (p {name: "Miceahl Cera"} ) RETURN p; // p is the var name and can be omitted
MATCH (m:Person), (m:Movie)
WHERE m.name='Micheal Cera' AND m.title='...'
CREATE (p)-[r:ACTED_IN { roles: ["Scott Pilgrim"] }]->(m)
RETURN r
MATCH (p:Person)->[:ACTED_IN]->(Movie)<-[:ACTED_IN]-(q:Person)
WHERE p.name = 'Name' and q.name <> 'Name'
RETURN q.name
```
```cypher
CREATE CONSTRAT ON (a:Movie) ASSERT a.title IS UNIQUE;
```
```cypher
MATCH (n) RETURN DISTINCT labels(n), count(*);
MATCH (n)-[r]->() RETURN type(r), count(*);
```
```cypher
MATCH path = shortestPath(
(p:Person {name: "Kevin Bacon"})-[*]-(q:Person {name: "Keanu Reeves"})
)
// no arrows - undirectional relationship
RETURN path;
// alternatively
UNWIND nodes(path) AS node
RETURN coalesce(node.name, node.title) AS nameOrTitle
MATCH (p:Person)-[:ACTED_IN*1..4]-(q:Person) // upper bounds of how far to jump
WHERE ...
RETURN DISTINCT q.name, count(*)
ORDER BY count(*) DESC, q.name;
```
```cypher
CREATE INDEX FOR (p:Person) ON p.born
```