Complete Intro to Databases

Neo4j: Adding Nodes & Relationships

Brian Holt

Brian Holt

Snowflake
Complete Intro to Databases

Check out a free preview of the full Complete Intro to Databases course

The "Neo4j: Adding Nodes & Relationships" Lesson is part of the full, Complete Intro to Databases course featured in this preview video. Here's what you'd learn in this lesson:

Brian explains that Cypher is the query language used for Neo4j, demonstrates how to add data to the Neo4j database, how to create a node, and how to add relationships to a given node. Data about the movie Scott Pilgrim vs. the World is added to the Neo4j database.

Preview
Close

Transcript from the "Neo4j: Adding Nodes & Relationships" Lesson

[00:00:00]
>> Let's get into some Neo4j. So this is our graph database that we're gonna be using today. The first thing I want you to do is I want you to just run this docker command here. This is gonna get our Neo4j database up and running. So this is gonna open port 7474, that's where the browser experience is exposed.

[00:00:19]
And the port 7687, which is actually the communication port where you can actually have your node app write to Neo4j. We're gonna say no authentication cuz we're just doing this locally for fun in our computer. Obviously, you would not do this production and have advanced credentials and things like that.

[00:00:38]
And we'll be on version 4.1.3. So I'm just gonna copy that command, have head over to my terminal, paste it in there. And now, if I say docker ps, you can see here, I got Neo4j running. And now, I'm gonna connect to it with something called cypher-shell. So I'm gonna copy this one here, paste that.

[00:01:02]
And that will now connect me to Neo4j and the cypher-shell. So what a cypher, cypher is actually the name of the query language for Neo4j, just like you have JavaScript for MongoDB and SQL for Postgres. The query language for Neo4j is called cypher. It's not the only query language for graph databases.

[00:01:24]
In fact, I think cypher actually just works on Neo4. But there's one called Gremlin that works with kind of a wider variety of databases including things like Cosmos DB. Okay, so let's get into actually creating our very first node in our database here. So we're gonna be talking about the movie Scott Pilgrim vs the World, which is like one of my favorite movies.

[00:01:51]
So I'm going to subject you to my personal preferences of movies and we're going to create our first node here. So I'm gonna say CREATE, and again, this is optional that you capitalize this. You can also say create just like that. I'm gonna capitalize it for your benefit.

[00:02:08]
CREATE, and we'll make a p:Person. So this p here just represents a variable. We're not gonna necessarily refer to it right now, but this :person right here, this is the label that we're gonna put on this particular node. So it's gonna be a person, and then we're gonna add some attributes to Scott.

[00:02:36]
Not Scott, Michael, Michael Cera. So I'm gonna say, name: "Michael Cera" like that, who plays Scott Pilgrim in Scott Pilgrim vs the World, and Michael was born in 1988. So these right here are the properties or attributes of this particular person node. So person here, this is called a label, and these are called the properties.

[00:03:11]
Okay, and then I'm gonna close that, put a semicolon. And now, I've added one node and set two properties and added a label to that. So now, what happens if I want a query for this particular node that I've made? I'm gonna say MATCH, P like this. So again, using a variable, you can say person.

[00:03:37]
This is just a variable name, so that we can refer to it later. I'm gonna put p cuz I'm lazy, and I'm gonna say name: "Michael Cera". And this is how I'm gonna query for that node that I just created. Here at the end, I need to put a curly brace there.

[00:03:54]
Here at the end, I'm gonna have to say RETURN cuz you have to do something with that node that we just created for it. I'm gonna say RETURN p, and you can see here, we have now query for this Michael Cera node that we've created. We could also have created for it just like this.

[00:04:08]
So p:Person like that, this is going to match every person in the database. Now, in this particular case, we just have one node in the database and it happens to be a person. But you can see there that also matches Michael Cera. Or if you wanna just get every single node, this is gonna match every single node, which again, we just have one node in the database, Michael Cera.

[00:04:33]
Okay, let's create a second node in our database. This node is actually going to represent the movie Scott Pilgrim vs the World. So we're gonna say CREATE m:Movie. Again, this m here just represents the movie that we're creating, it represents the variable, right? Or it is the variable rather.

[00:04:54]
And then we just have to give it all the attributes that we wanna give it. So create movie with a title of Scott Pilgrim vs the World. It was released in 2010. Its tagline, and I'm not making this up, this is actually the one that someone in marketing [LAUGH] chose for Scott Pilgrim vs the World.

[00:05:21]
Tagline is An epic of epic epicness. Someone was like really self-impressed when they came up with that line. Okay, and then we'll close our object there. So that'll be all of the properties, and then we'll close that. And then here, if you want to, you can say RETURN m;.

[00:05:48]
This will both create it and then will return it, so that we can inspect it right. So now, we can see, here's the movie thing that we just created. It added one node, set three properties, and added one label. Cool. So now, we have two nodes. So if I run that same query, MATCH (p) RETURN p;, I should get two things back, one person and one movie.

[00:06:06]
As you can see here, I get one person and one movie. Now, I wanna create a relationship between Scott Pilgrim and Michael Cera. I wanna say that Michael Cera acted in and that acted in is gonna be the relationship or the edge between those two things, that Michael Cera acted in Scott Pilgrim vs the World.

[00:06:27]
So the first thing we have to do is we have to match both of those nodes. So I'm gonna say MATCH, (Michael:Person), (ScottVsWorld:, which is a movie. Now honestly, I could stop here cuz I literally only have one person and one movie. But let's go ahead and make this a little bit more qualified.

[00:06:56]
So I'm gonna add a where clause. I'm gonna say WHERE Michael, Again, Michael just being a variable that represents Michael Cera, .name = 'Michael Cera' AND ScottVsWorld.title. I did call it title, right? I did. Okay, cool. Title = 'Scott Pilgrim vs the World', okay? And I'm gonna say, CREATE a relationship with Michael on one end.

[00:07:41]
I'm gonna put a- here, then I'm gonna put a relationship between the two. So I'm gonna do that with a kinda square bracket, and I'm gonna call this r, you can call this a relationship or something like that. I'm gonna say relationship:ACTED_IN. So the important part is here, this relationship, again, represents a variable.

[00:08:00]
ACTED_IN here, this represents the type of relationship that we have here. So the type of relationship is that Michael Cera acted in Scott Pilgrim vs the World. And this also can have properties of its own. So the role that Michael Cera had in Scott Pilgrim vs the World, if I do this, Is he had the role of Scott Pilgrim.

[00:08:31]
Now, this is actually gonna be an array here cuz yeah, as you can imagine, if we're doing the Nutty Professor, Eddie Murphy had like five roles in that movie, right? In this particular case, Michael Cera was just Scott Pilgrim. So we'll just close it at that. Okay, that's all the properties that we're gonna put here in this relationship, but we could put as many as we want.

[00:08:55]
And then we have to close the square bracket. And then we have to describe, again, this is a relationship. So I'm gonna put another- here, but I wanna describe the cardinality of the direction of this particular relationship. It's Michael Cera acted in Scott Pilgrim. So there's a direction with that.

[00:09:15]
So I'm gonna put this arrow here, right? Again, this is just a space with an angle bracket at the end of it. But when I put those together, my font combines them. It actually looks pretty nice with this particular set up here. So Scott Pilgrim here is gonna be ScottVsWorld.

[00:09:33]
This is gonna represent the movie, right? Now, I can hit semi-colon here and hit Enter, this would be done. But I just wanna return the relationship here, so I can investigate that. So if I put semicolon here now, you can see here it created one relationship and set one property.

[00:09:53]
And the relationship that I've created here is an ACTED_IN relationship of Scott Pilgrim. So this is one of the things that I like about cypher is a query language. This is actually based on ASCII art and they actually explicitly say that they did this so that you kind of read it like a picture.

[00:10:09]
So we have one end of this relationship, and then we have this particular thing here. In fact, let me just copy and paste it so you can see here, kind of in a vacuum. It's meant to read like a big arrow. In fact, if you just kinda like pared this down a little bit, It's supposed to look like this where you have like (a), then a relationship to (b), right?

[00:10:36]
So it's meant to kinda like read like a big arrow. But when you have a bunch of stuff in here, it kinda gets like drawn out, right? Cool, last thing I wanted to kinda call out here is kind of this, you'll notice the case in here that it looks a little weird.

[00:10:54]
So one, these relationships are always described in screaming case, right, where everything is all uppercase. It doesn't have to be that way. That's just kind of conventions that it's really easy to see that this is a relationship type. Whereas you look at something like the label here like this one, it's in capital case again.

[00:11:15]
So that's very easy to recognize at a glance that this is a label type, not a relationship type. It's something that people in the Neo4j community do. It's something that the docks recommend. So I just recommend that you do that, too. So the next thing is we're going to copy and paste this really long query here, That adds the rest of the actors and actresses and directors to Scott Pilgrim vs the World.

[00:11:42]
So, oop, I guess, I hold on, that's gonna fail. I'll try that again. So if we do that, that added 7 nodes created, 7 relationships, and set 20 properties. You can see here, we added Aubrey Plaza, Brie Larson, Anna Kendrick, all these actors and actresses to Scott Pilgrim vs the World.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now