From the previous article, we learned what Neo4j is, why to use it, what scenarios to use it in, and how to install it. If you don’t want to be familiar with it, click here to send it

Since Neo4j is a graph database, there is no doubt that adding, deleting, modifying and checking are essential. In this article, we will learn the basic operations of Neo4j on nodes and relationships.

First of all, we open the secondary browser console (http://xxx.xxx.xxx.xxx:7474/browser), the user name is secondary, the default password is secondary, if you already have a password, then enter you modify the password. Log in and we’ll see the following screen,

That’s right, enter the CQL statement in everyone’s favorite dollar sign. Here we take students and teachers as examples.

1. Add nodes

Neo4j uses the create command to add, which is similar to insert in MySQL.

1. Create a student node (only nodes, no attributes) :

create (s:Student)Copy the code

After the dollar sign enters the above CQL, press Enter or click the triangle execute button on the right, you will see the following result:

This indicates that we have created the student node.

The syntax for create is as follows:

create (<node-name>:<label-name>)Copy the code

  • Node-name: this is the name of the node we want to create
  • Label-name: This is the name of the label we want to create

2. Create a student node (create node with properties)

Create a node where id = 10000, name = Zhang SAN, age = 18, gender = male

Create (s:Student{id:10000, name:" zhang ",age:18,sex:1})Copy the code

After the command is executed, the following result is displayed:

This indicates that we have created an S node with four attributes id, name, age, sex.

Id, name, age, sex, etc.

The syntax for creating a node with attributes is as follows:

create (<node-name>:<label-name> { <property1-name>:<property1-Value>, <property2-name>:<property2-Value>, ... , <property3-name>:<property3-Value> })Copy the code

Property1-name indicates the property name and property1-value indicates the property Value.

Second, the query

We created nodes with no attributes and nodes with attributes in the previous step, so the question is, how do we check? Query cough up ~

Neo4j uses match… return … The MySQL select command is used for query.

We query the information about the newly created node.

1. Query all students

match (s:Student) return sCopy the code

You can see from the figure above that we just created two nodes, one with no attributes and one with attributes. The two nodes are shown in the form of graphs. We can also switch Graph, Table, Text, etc. on the left to display them in different forms.

2. Query all or some fields

All you need to do is concatenate the fields to be displayed with the node name + dot + attribute fields as follows:

match (s:Student) return s.id,s.name,s.age,s.sexCopy the code

This makes it clear that we inserted the student attribute information. Because there is a node with no attributes, the values displayed in row 2 of the table are null.

3. Query information about students whose age is 18

match (s:Student) where s.age=18 return s.id,s.name,s.age,s.sexCopy the code

How about, this conditional query is similar to MySQL. Of course there are sorting, grouping, federation, paging, and so on. To better demonstrate these, let’s first insert some data, one by one:

Create (s:Student{id:10001, name:" lI si ",age:18,sex:1}) return s create (s:Student{id:10002, age:18,sex:1}) Name :" wang ",age:19,sex:1}) return s create (s:Student{id:10003, Name :" zhao liu ",age:20,sex:1}) return s create (s:Student{id:10004, Name :" 四 七",age:17,sex:0}) return s create (s:Student{id:10005, Name :" sun ",age:23,sex:1}) return s create (s:Student{id:10006, Name :" u ",age:15,sex:1}) return s create (s:Student{id:10007, Name :" c ",age:19,sex:0}) return s create (s:Student{id:10008, Name :" xu xi ",age:18,sex:1}) return s create (s:Student{id:10009, Name :" zhushi ",age:21,sex:1}) return s create (s:Student{id:10010, name:" zhushi ",age:22,sex:1}) return sCopy the code

We add a return to the create statement, which means you should return the data to me after I insert it, like this:

In this way, our data is built, we can query all of the first look:

match (s:Student) return s.id,s.name,s.age,s.sexCopy the code

In the lower left corner of the figure, we can see that there are 12 pieces of data. One with no attributes plus 11 with attributes.

Select * from male (sex=1) select * from male (sex=1)

match (s:Student) where s.sex=1 return s.id,s.name,s.age,s.sex order by s.age descCopy the code

It’s clear. It’s in reverse chronological age.

5. The name is not null and is grouped by gender

CQL does not explicitly write a group by field. The group by field is automatically determined by the interpreter, that is, the field without aggregation function is automatically determined as a group field.

match (s:Student) where s.name is not null return s.sex,count(*)Copy the code

It is not hard to see that the above is grouped by the sex field.

Select * from student where gender is male or female and age is 19;

match (s:Student) where s.sex=1 and s.age=19 return s.id,s.name,s.sex,s.age 
union 
match (s:Student) where s.sex=0 and s.age=19 return s.id,s.name,s.sex,s.age Copy the code

There’s a union, there’s a union all, and the difference between the two is the same as in SQL.

  • Union: Performs a union operation on two result sets, excluding duplicate rows;
  • Union all: Performs a union operation on two result sets, including repeating rows;

7. Paging query (4 items per page, query the data on page 3)

match (s:Student) return s.id,s.name,s.sex,s.age skip 8 limit 4Copy the code

Skip in the above CQL indicates how many items to skip and limit indicates how many items to get. Four items per page, query the data on the third page, so skip the first eight items, query four items, or start with the eighth item, not including the eighth item, and then query four items.

8. In operation (query two data with ID 10001 and 10005)

Match (s:Student) where s.id in [10001,10005] return s.id,s.name, s.ex,s.ageCopy the code

Notice that I’m using brackets here, which is different from SQL.

Third, increase relationships

Above we covered adding individual nodes and queries. Here we introduce adding relationships. To have a relationship, we create a teacher node.

Create a male teacher Wang who is 35 years old and teaches Chinese:

Create (t:Teacher{id:20001,name:" wang ",age:35,sex:1,teach:" Language "}) return tCopy the code

1. Suppose there are three students in teacher Wang’s class: Zhang SAN, Li Si and Wang Wu. Here we need to create the relationship between Teacher Wang and the three students.

match (t:Teacher),(s:Student) where t.id=20001 and s.id=10000 
create (t)-[teach:Teach]->(s)
return t,teach,sCopy the code

In this way, the relationship between Teacher Wang and Zhang SAN was created. Next, we continue to create the relationship between Teacher Wang and Li Si and Wang Wu.

match (t:Teacher),(s:Student) where t.id=20001 and s.id=10001 
create (t)-[teach:Teach]->(s)
return t,teach,sCopy the code

match (t:Teacher),(s:Student) where t.id=20001 and s.id=10002 
create (t)-[teach:Teach]->(s)
return t,teach,sCopy the code

As you can see, the syntax for creating relationships is as follows:

match (<node1-label-name>:<node1-name>),(<node2-label-name>:<node2-name>) 
where <condition>
create (<node1-label-name>)-[<relationship-label-name>:<relationship-name>]
    ->(<node2-label-name>) 
或者
match (<node1-label-name>:<node1-name>),(<node2-label-name>:<node2-name>) 
where <condition>
create (<node1-label-name>)-[<relationship-label-name>:<relationship-name>
       {<relationship-properties>}]->(<node2-label-name>) Copy the code

  • Node1-name indicates the node name, and label1-name indicates the label name
  • Relationship-name specifies the node name of the relationship, and relationship-label-name specifies the label name of the relationship
  • Node2-name specifies the node name, and label2-name specifies the label name

The relationship between teachers and students has increased. Let’s check:

match (t:Teacher)-[teach:Teach]-(s:Student) return t,teach,sCopy the code

The relationship is obvious. Teacher Wang teaches Zhang SAN, Li Si and Wang Wu.

2. We establish a relationship between Guangdong and Shenzhen, which belongs to Guangdong province. But there is no Guangdong province node and Shenzhen node. Yes, we just create a relationship for two non-existent nodes.

Create (c: City {id: 30000, name: "shenzhen"}) - [belongto: belongto {type: "belong to"}] - > (p: Province {id: 40000, name: "guangdong Province"})Copy the code

Let’s check the relationship between Shenzhen and Guangdong that we created.

Match (c:City{id:30000,name:" shenzhen "})-[belongto: belongto {type:" belong "}]->(p: type {id:40000,name:" Guangdong "}) return c,belongto,pCopy the code

The syntax for creating a relationship between two nonexistent nodes is as follows:

create (<node1-name>:<label1-name>
    {<property1-name>:<property1-Value>,
    <property1-name>:<property1-Value>})-
[(<relationship-name>:<relationship-label-name>{<property-name>:<property-Value>})]
->(<node2-name>:<label2-name>
    {<property1-name>:<property1-Value>,
    <property1-name>:<property1-Value>})Copy the code

Of course, the attributes are optional, just to be more accurate.

If we want to query all the relationships in Neo4j, we need to write CQL as follows:

match (a)-[b]-(c) return a,b,cCopy the code

Three, modify,

The modification in Neo4j is also very similar to that in SQL, which uses the set clause. Like ES, the Neo4j CQL set clause can add new attributes to existing nodes or relationships.

Through the above query, we have already remembered that the age of student Zhang SAN is 18 years old. It is 2020, and Zhang SAN is one year older, so we need to change zhang SAN’s age to 19.

Match (s:Student) where s.name=" c "set s.age=19 return sCopy the code

In the red box above, we can clearly see that John’s age has been updated to 19.

Four, delete,

The deletion in Neo4j is also very similar to that in SQL, both of which are DELETE. Of course, in addition to delete, there is also remove delete.

1. Delete a node

Here is an example to delete the student node without attributes:

Query nodes without attributes in students first

match (s:Student) where s.name is null return s Copy the code

Then we delete this node:

match (s:Student) where s.name is null delete s Copy the code

Change the CQL return to delete.

After executing the above CQL deletion, let’s query again:

There are no student nodes without attributes, which indicates that we have successfully deleted them.

2. Delete the associated node

Here we delete the relationship between Guangdong and Shenzhen as an example:

Match (c:City{id:30000,name:" shenzhen "})-[belongto]->(p: type {id:40000,name:" Guangdong "}) return c,belongto,pCopy the code

There is data at this point.

Then we perform the following delete CQL, change the return of the above query CQL to delete:

Match (c:City{id:30000,name:" shenzhen "})-[belongto]->(p:Province{id:40000,name:" Guangdong "}) delete c,belongto,pCopy the code

After executing the above deletion of CQL, let’s query again:

It is found that the two nodes and relationship of Guangdong and Shenzhen no longer exist.

3. Delete all nodes

This CQL is mainly used for testing, but should not be executed in production environment, otherwise, it will be deleted from the library to run

match (n) detach delete nCopy the code

This CQL will not be demonstrated.

4. Delete the existing attributes of the node or relationship

Existing attributes of a node or relationship can be removed by remove.

For example, we delete the sex property from the student li node:

match (s:Student{id:10001}) remove s.sexCopy the code

Remove CQL;

The sex attribute of li4 is null.

Five, the summary

This article introduces CQL in Neo4j, as well as add, delete, change and check. We also make a comparison with some SQL in MySQL in practice. The next article continues to introduce the application of neo4j in Springboot.

This article is published by OpenWrite!