Skip to content

Instantly share code, notes, and snippets.

@blacksmithop
Created May 3, 2024 05:26
Show Gist options
  • Save blacksmithop/a31c2cb970b21e324e8a460fffa1a126 to your computer and use it in GitHub Desktop.
Save blacksmithop/a31c2cb970b21e324e8a460fffa1a126 to your computer and use it in GitHub Desktop.
Neo4j Introduction
CHEAT SHEET
1. Database Creation
CREATE DATABASE SampleGraphDatabase
This creates a database named SampleGraphDatabase
2. View data
Currently our database is empty
3. Insert data
Data is stored in Neo4j as nodes. Each node has a label and properties.
Think of labels as identifiers for a node. It tells us what type of node it is.
If you are familiar with SQL, you would know about columns. Properties are similar to columns
Let us create a Person node and give it a property called name.
CREATE (p: Person {name: "Abhinav", userID: 1})
4. Fetch data
Let's fetch the data we just inserted
MATCH (n:Person) RETURN n
Here's the node in detail. As you can see each node columns with an id and elementid property by default.
Think of <id> as the ID field in SQL, we will be using this in a lot of queries.
name is the property we added to that node.
Now that we have a added a node, it will show up under Datatbase Information.
5. Node Relationship
We can create relationships between nodes having different labels.
First, let us create another node.
CREATE (AdminProfile: Profile {created_date: datetime("2024-06-30T18:40:32.142+0100"), userID: 0})
CREATE (ADMIN: Person{name: "Admin User"})
Now let us create a relationship called IsOwnerOf
MATCH (a:Person),
(b:Profile)
WHERE a.userID = b.userID
CREATE (a)-[:IsOwnerOf]->(b);
Now we have established a relationship between Profile and Person nodes based on their shared userID .
6. Fetch data with filters
We can filter our query results with a WHERE clause
MATCH (n) WHERE n.userID = 1 RETURN n
As you can see these nodes have a relation between them.
Here's the entire database
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment