/neo4j-notes

Neo4j graph database notes for learning

MIT LicenseMIT

Neo4j Notes

Neo4j is a native graph database that lets us build nodes and relationships effortlessly.

Terms

Cypher: is a query language in neo4j that lets us query data from a graph.

Run Neo4j locally

To run neo4j locally install docker on your os and run the following command that will pull the docker image and run it,

docker run -dit --rm --name=my-neo4j -p 7474:7474 -p 7687:7687 --env=NEO4J_AUTH=none neo4j

flags explanation:

  • -p 7474:7474 exposes the Neo4j browser interface on port 7474.
  • -p 7687:7687 exposes the Bolt protocol for Cypher queries on port 7687.
  • --env=NEO4J_AUTH= sets the default username and password (neo4j/test).

to run neo4j REPL run the following command that will let us interact with the database using cypher or commands

docker exec -it my-neo4j cypher-shell

OR

winpty docker exec -it my-neo4j cypher-shell

Basic Cypher Queries

Insert a node:

Syntax for inserting a node

CREATE (variable_name: LABEL { ...props });

example:

CREATE (s1:Site {url: 'https://www.google.com', title: 'Google', description: 'The world\'s most popular search engine.'});

example of node insertion with timestamp,

CREATE (f:Folder { name: 'Tech', createdAt: datetime() });

Visualize your data:

Neo4j provides a browser experience that allows us to see a visual representation of our data. to open it go to http://localhost:7474/browser/ and set the authentication type to none since we are just running locally.

to see all the data paste MATCH (n) RETURN n inside the code cell and run it

image

Relationships

Syntax to create relationship:

CREATE (NODE)-[:LABEL]->(NODE)
  • NODE: Represents a node in the graph.
  • LABEL: Represents the type of relationship between nodes.

In our case, A folder can contain many sites. To link sites to the folder run the following query,

MATCH (f:Folder {name: 'Tech'}), (s:Site)
WHERE s.url IN ['https://www.github.com', 'https://www.stackoverflow.com']
CREATE (f)-[:CONTAINS]->(s)
RETURN f, s;

image

Nested relationship

To create a nested relationship where a folder is inside another folder, run the following query:

MATCH (f:Folder) 
WHERE f.name = 'Tech' 
CREATE (newFolder:Folder { name: 'Web', createdAt: datetime() })
CREATE (f)-[:CONTAINS]->(newFolder)
RETURN f, newFolder;

image

To insert a new site in a nested folder run:

MATCH (f:Folder) WHERE f.name = 'Web'
CREATE (s:Site {
  url: 'https://developer.mozilla.org/en-US/', 
  title: 'MDN', 
  description: 'The MDN Web Docs site provides information about Open Web technologies including HTML, CSS, and APIs for both Web sites and progressive web apps' 
})
CREATE (f)-[:CONTAINS]->(s)
RETURN f,s;

Query to add multiple sites at once:

MATCH (f:Folder) WHERE f.name = 'Web'
UNWIND [
  { url: 'https://reactjs.org/', title: 'React', description: 'A JavaScript library for building user interfaces.' },
  { url: 'https://nodejs.org/', title: 'Node.js', description: 'A JavaScript runtime built on Chrome\'s V8 JavaScript engine.' },
  { url: 'https://docs.nestjs.com/', title: 'NestJS', description: 'A progressive Node.js framework for building efficient, reliable, and scalable server-side applications.' }
] AS site
CREATE (s:Site {
  url: site.url, 
  title: site.title, 
  description: site.description
})
CREATE (f)-[:CONTAINS]->(s)
RETURN f, s;

In Neo4j's Cypher query language, the UNWIND clause is used to transform a list of values into individual rows. Essentially, it "unwraps" a list so that each item in the list becomes a separate row in the result set. This is particularly useful when you want to perform operations on multiple items within a single query.

image

Relationship Queries

Query to get all descendants of a node,

MATCH (f:Folder)
WHERE f.name = 'Tech'
OPTIONAL MATCH (f)-[:CONTAINS*]->(descendants)
RETURN DISTINCT descendants;

image

We can also specify a depth how deep we want to query using this query: In this case, we are specifying depth 1 and saying just querying direct children.

MATCH (f:Folder)
WHERE f.name = 'Tech'
OPTIONAL MATCH (f)-[:CONTAINS*1]->(descendants)
RETURN DISTINCT descendants;

image


More Coming Up... Stay tuned!