cypher
This commit is contained in:
parent
634ccd98a8
commit
c6414868c5
@ -6,108 +6,162 @@
|
|||||||
- A Relationship can have Properties and Types.
|
- A Relationship can have Properties and Types.
|
||||||
|
|
||||||
### delete whole db content
|
### delete whole db content
|
||||||
|
|
||||||
```MATCH (n) detach delete n;```
|
```MATCH (n) detach delete n;```
|
||||||
|
|
||||||
### Nodes
|
### Nodes
|
||||||
|
|
||||||
#### Create
|
#### Create
|
||||||
|
|
||||||
Create a node with label "Student" and properties name, age and nicknames (as array).
|
Create a node with label "Student" and properties name, age and nicknames (as array).
|
||||||
|
|
||||||
```CREATE (n :Student {name:"Jack",age:22,nicknames:["Jacko", "Jackie"]});```
|
```CREATE (n :Student {name:"Jack",age:22,nicknames:["Jacko", "Jackie"]});```
|
||||||
|
|
||||||
#### Merge
|
#### Merge
|
||||||
|
|
||||||
Merge erstellt wie Create ein neuer Node. Wenn der Node aber schon existiert, geschieht nichts, im Gegensatz zu Create, wo ein zweiter Node erstellt wird. Merge kann auch verwendet werden, um zusätzliche Properties hinzu zu fügen. Entweder, wenn der Node schon existiert, oder zusammen mit der Neuerstellung, falls der Node noch nicht existiert.
|
Merge erstellt wie Create ein neuer Node. Wenn der Node aber schon existiert, geschieht nichts, im Gegensatz zu Create, wo ein zweiter Node erstellt wird. Merge kann auch verwendet werden, um zusätzliche Properties hinzu zu fügen. Entweder, wenn der Node schon existiert, oder zusammen mit der Neuerstellung, falls der Node noch nicht existiert.
|
||||||
|
|
||||||
Hier wird, falls der Node existiert, nicknames überschrieben (falls diese Property vorhanden ist)
|
Hier wird, falls der Node existiert, nicknames überschrieben (falls diese Property vorhanden ist)
|
||||||
|
|
||||||
```MERGE (n :Student {name:"Jack",age:22}) S
|
```MERGE (n :Student {name:"Jack",age:22}) S
|
||||||
ET n.nicknames=["J"];
|
ET n.nicknames=["J"];
|
||||||
oder explizit
|
oder explizit
|
||||||
MERGE (n :Student {name:"Jack",age:22})
|
MERGE (n :Student {name:"Jack",age:22})
|
||||||
ON MATCH SET n.nicknames=["J"];```
|
ON MATCH SET n.nicknames=["J"];```
|
||||||
|
|
||||||
Soll nicknames nur hinzugefügt werden, wenn der Node noch nicht existiert, dann so:
|
Soll nicknames nur hinzugefügt werden, wenn der Node noch nicht existiert, dann so:
|
||||||
|
|
||||||
```MERGE (n :Student {name:"Jack",age:22})
|
```MERGE (n :Student {name:"Jack",age:22})
|
||||||
ON CREATE SET n.nicknames=["J"];```
|
ON CREATE SET n.nicknames=["J"];```
|
||||||
|
|
||||||
Kombiniert:
|
Kombiniert:
|
||||||
|
|
||||||
```MERGE (n :Student {name:"Jack",age:22})
|
```MERGE (n :Student {name:"Jack",age:22})
|
||||||
ON CREATE SET n.nicknames=["Jacko"]
|
ON CREATE SET n.nicknames=["Jacko"]
|
||||||
ON MATCH SET n.nicknames=["Jackie"];```
|
ON MATCH SET n.nicknames=["Jackie"];```
|
||||||
|
|
||||||
#### Select
|
#### Select
|
||||||
|
|
||||||
Match all nodes with label "Student", show name and first of the nickname(s)
|
Match all nodes with label "Student", show name and first of the nickname(s)
|
||||||
|
|
||||||
```MATCH (n :Student) RETURN n.name, n.nicknames[0];```
|
```MATCH (n :Student) RETURN n.name, n.nicknames[0];```
|
||||||
```MATCH (rog :Student {name: "Roger"}), (tim :Student {name: "Timothy"}) RETURN rog, tim```
|
```MATCH (rog :Student {name: "Roger"}), (tim :Student {name: "Timothy"}) RETURN rog, tim```
|
||||||
|
|
||||||
##### Where clause
|
##### Where clause
|
||||||
|
|
||||||
Match all nodes with label "Student" and name "Tim"
|
Match all nodes with label "Student" and name "Tim"
|
||||||
|
|
||||||
```MATCH (n :Student { name:"Roger" } ) RETURN n;
|
```MATCH (n :Student { name:"Roger" } ) RETURN n;
|
||||||
is the same as:
|
is the same as:
|
||||||
MATCH (n :Student ) WHERE n.name="Roger" RETURN n;```
|
MATCH (n :Student ) WHERE n.name="Roger" RETURN n;```
|
||||||
|
|
||||||
###### AND
|
###### AND
|
||||||
|
|
||||||
```MATCH (n :Student ) WHERE n.name="Roger" AND n.age=42 RETURN n;```
|
```MATCH (n :Student ) WHERE n.name="Roger" AND n.age=42 RETURN n;```
|
||||||
|
|
||||||
###### OR
|
###### OR
|
||||||
|
|
||||||
```MATCH (n :Student ) WHERE n.name="Roger" OR n.age=21 RETURN n;```
|
```MATCH (n :Student ) WHERE n.name="Roger" OR n.age=21 RETURN n;```
|
||||||
|
|
||||||
###### NOT
|
###### NOT
|
||||||
|
|
||||||
```MATCH (n) WHERE n.name<>"Roger" AND n.name<>"Josh" RETURN n;
|
```MATCH (n) WHERE n.name<>"Roger" AND n.name<>"Josh" RETURN n;
|
||||||
oder
|
oder
|
||||||
MATCH (n) WHERE NOT n.name="Roger" AND NOT n.name="Josh" RETURN n;```
|
MATCH (n) WHERE NOT n.name="Roger" AND NOT n.name="Josh" RETURN n;```
|
||||||
|
|
||||||
###### Smaller/Larger
|
###### Smaller/Larger
|
||||||
|
|
||||||
```MATCH (n) WHERE n.age>=21 RETURN n;```
|
```MATCH (n) WHERE n.age>=21 RETURN n;```
|
||||||
|
|
||||||
#### Update
|
#### Update
|
||||||
##### Update Node Property ===
|
|
||||||
|
##### Update Node Property
|
||||||
|
|
||||||
Student "Tim" name auf "Timothy" ändern.
|
Student "Tim" name auf "Timothy" ändern.
|
||||||
|
|
||||||
```MATCH (n :Student { name:"Tim" }) SET n.name="Timothy" RETURN n;```
|
```MATCH (n :Student { name:"Tim" }) SET n.name="Timothy" RETURN n;```
|
||||||
|
|
||||||
##### Update Label
|
##### Update Label
|
||||||
|
|
||||||
Give the label with name "Roger" the labels "Student" and "Teacher".
|
Give the label with name "Roger" the labels "Student" and "Teacher".
|
||||||
|
|
||||||
```MATCH (n {name:"Roger"}) SET n:Student, n:Teacher RETURN n;```
|
```MATCH (n {name:"Roger"}) SET n:Student, n:Teacher RETURN n;```
|
||||||
|
|
||||||
#### Remove
|
#### Remove
|
||||||
|
|
||||||
Entfernen zweier Labels vom Node:
|
Entfernen zweier Labels vom Node:
|
||||||
|
|
||||||
```MATCH (n :Student {name:"Roger"}) REMOVE n:Teacher, n:Student RETURN n;```
|
```MATCH (n :Student {name:"Roger"}) REMOVE n:Teacher, n:Student RETURN n;```
|
||||||
|
|
||||||
Entfernen der Property address:
|
Entfernen der Property address:
|
||||||
|
|
||||||
```MATCH (n :Student {name:"Roger"}) REMOVE n.address RETURN n;```
|
```MATCH (n :Student {name:"Roger"}) REMOVE n.address RETURN n;```
|
||||||
|
|
||||||
#### Delete
|
#### Delete
|
||||||
|
|
||||||
Löschen eines Nodes
|
Löschen eines Nodes
|
||||||
|
|
||||||
```MATCH (n :User) DELETE n RETURN n;```
|
```MATCH (n :User) DELETE n RETURN n;```
|
||||||
|
|
||||||
### Relations
|
### Relations
|
||||||
|
|
||||||
```
|
```
|
||||||
(node1)-[:FRIEND]->(node2) --> Uni-Directional
|
(node1)-[:FRIEND]->(node2) --> Uni-Directional
|
||||||
(node1)<-[:FRIEND]-(node2) --> Uni-Directional
|
(node1)<-[:FRIEND]-(node2) --> Uni-Directional
|
||||||
(node1)-[:FRIEND]-(node2) --> Bi-Directional
|
(node1)-[:FRIEND]-(node2) --> Bi-Directional
|
||||||
```
|
```
|
||||||
|
|
||||||
`[:FRIEND]` ist ein Type. Wie ein Label bei den Nodes. Aber im Gegensatz zu Labelnodes ist nur ein Type pro Relationship möglich.
|
`[:FRIEND]` ist ein Type. Wie ein Label bei den Nodes. Aber im Gegensatz zu Labelnodes ist nur ein Type pro Relationship möglich.
|
||||||
|
|
||||||
#### Create
|
#### Create
|
||||||
|
|
||||||
Create a Uni-Directional Relationship between Roger and Timothy called "Friend".
|
Create a Uni-Directional Relationship between Roger and Timothy called "Friend".
|
||||||
|
|
||||||
```MATCH (rog :Student {name: "Roger"}), (tim :Student {name: "Timothy"})
|
```MATCH (rog :Student {name: "Roger"}), (tim :Student {name: "Timothy"})
|
||||||
CREATE (rog)-[:FRIEND]->(tim)
|
CREATE (rog)-[:FRIEND]->(tim)
|
||||||
RETURN rog, tim```
|
RETURN rog, tim```
|
||||||
|
|
||||||
Hier noch mit einer Relation Property
|
Hier noch mit einer Relation Property
|
||||||
|
|
||||||
```MATCH (rog :Student {name: "Roger"}), (tim :Student {name: "Timothy"})
|
```MATCH (rog :Student {name: "Roger"}), (tim :Student {name: "Timothy"})
|
||||||
CREATE (rog)-[r :FRIEND {nickname:"Timmi"}]->(tim)
|
CREATE (rog)-[r :FRIEND {nickname:"Timmi"}]->(tim)
|
||||||
RETURN rog, r, tim```
|
RETURN rog, r, tim```
|
||||||
|
|
||||||
Um eine Bi-Directional Relation zu machen, muss einfach noch der umgekehrte Weg gemacht werden:
|
Um eine Bi-Directional Relation zu machen, muss einfach noch der umgekehrte Weg gemacht werden:
|
||||||
|
|
||||||
```MATCH (rog :Student {name: "Roger"}), (tim :Student {name: "Timothy"})
|
```MATCH (rog :Student {name: "Roger"}), (tim :Student {name: "Timothy"})
|
||||||
CREATE (rog)<-[r :FRIEND {nickname:"Rocket"}]-(tim)
|
CREATE (rog)<-[r :FRIEND {nickname:"Rocket"}]-(tim)
|
||||||
RETURN rog, r, tim```
|
RETURN rog, r, tim```
|
||||||
|
|
||||||
Bi-Directional auf einen Chlapf:
|
Bi-Directional auf einen Chlapf:
|
||||||
|
|
||||||
```MATCH (rog :Student {name: "Roger"}), (jac :Student {name: "Jack"})
|
```MATCH (rog :Student {name: "Roger"}), (jac :Student {name: "Jack"})
|
||||||
CREATE (rog)-[r1 :FRIEND {nickname:"Rocket"}]->(jac)<-[r2 :FRIEND {nickname:"jaccc"}]-(rog)
|
CREATE (rog)-[r1 :FRIEND {nickname:"Rocket"}]->(jac)<-[r2 :FRIEND {nickname:"jaccc"}]-(rog)
|
||||||
RETURN rog, r1, r2, jac```
|
RETURN rog, r1, r2, jac```
|
||||||
|
|
||||||
#### Select
|
#### Select
|
||||||
|
|
||||||
Select Relationship between 2 nodes
|
Select Relationship between 2 nodes
|
||||||
|
|
||||||
```MATCH (rog :Student {name:"Roger"})-[r]-(tim :Student {name:"Timothy"})
|
```MATCH (rog :Student {name:"Roger"})-[r]-(tim :Student {name:"Timothy"})
|
||||||
RETURN rog, r, tim```
|
RETURN rog, r, tim```
|
||||||
|
|
||||||
#### Update
|
#### Update
|
||||||
|
|
||||||
Update Relationship Property
|
Update Relationship Property
|
||||||
|
|
||||||
```MATCH (:Student {name: "Roger"})<-[r:FRIEND]-(:Student {name: "Timothy"})
|
```MATCH (:Student {name: "Roger"})<-[r:FRIEND]-(:Student {name: "Timothy"})
|
||||||
SET r.nickname = "Rog"
|
SET r.nickname = "Rog"
|
||||||
RETURN r```
|
RETURN r```
|
||||||
|
|
||||||
#### Delete
|
#### Delete
|
||||||
|
|
||||||
Löschen einer Relationship. Roger will nicht mehr Freund von Timothy sein.
|
Löschen einer Relationship. Roger will nicht mehr Freund von Timothy sein.
|
||||||
|
|
||||||
```MATCH (rog :Student {name:"Roger"})-[r:FRIEND]->(tim :Student {name:"Timothy"})
|
```MATCH (rog :Student {name:"Roger"})-[r:FRIEND]->(tim :Student {name:"Timothy"})
|
||||||
DELETE r
|
DELETE r
|
||||||
RETURN rog, r, tim```
|
RETURN rog, r, tim```
|
||||||
|
|
||||||
oder für alle Directions:
|
oder für alle Directions:
|
||||||
|
|
||||||
```MATCH (rog :Student {name:"Roger"})-[r:FRIEND]-(tim :Student {name:"Timothy"})
|
```MATCH (rog :Student {name:"Roger"})-[r:FRIEND]-(tim :Student {name:"Timothy"})
|
||||||
@ -115,24 +169,31 @@ DELETE r
|
|||||||
RETURN rog, r, tim```
|
RETURN rog, r, tim```
|
||||||
|
|
||||||
### Node and Relation
|
### Node and Relation
|
||||||
|
|
||||||
#### Create
|
#### Create
|
||||||
|
|
||||||
Create 2 nodes and relations
|
Create 2 nodes and relations
|
||||||
|
|
||||||
```
|
```
|
||||||
CREATE p = (da :Student {name:"Daisy", age:30})-[:FRIEND {nickname:"Annabella"}]->(a :Student {name:"Anna", age:31})-[:FRIEND {nickname:"Day"}]->(da)
|
CREATE p = (da :Student {name:"Daisy", age:30})-[:FRIEND {nickname:"Annabella"}]->(a :Student {name:"Anna", age:31})-[:FRIEND {nickname:"Day"}]->(da)
|
||||||
RETURN p;
|
RETURN p;
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Merge
|
#### Merge
|
||||||
|
|
||||||
Used if you don't want to create new entries if it's alreedy there. Doing the following query multiple times will result only in one relationship, whereas CREATE would create new relationships each time.
|
Used if you don't want to create new entries if it's alreedy there. Doing the following query multiple times will result only in one relationship, whereas CREATE would create new relationships each time.
|
||||||
|
|
||||||
Here, Roger and Timothy already exist:
|
Here, Roger and Timothy already exist:
|
||||||
|
|
||||||
```
|
```
|
||||||
MATCH (rog :Student {name: "Roger"}), (tim :Student {name: "Timothy"})
|
MATCH (rog :Student {name: "Roger"}), (tim :Student {name: "Timothy"})
|
||||||
MERGE (rog)<-[r1 :CLASSMATE]-(tim)
|
MERGE (rog)<-[r1 :CLASSMATE]-(tim)
|
||||||
MERGE (rog)-[r2 :CLASSMATE]->(tim)
|
MERGE (rog)-[r2 :CLASSMATE]->(tim)
|
||||||
RETURN rog, tim, type(r1), r1, type(r2), r2;
|
RETURN rog, tim, type(r1), r1, type(r2), r2;
|
||||||
```
|
```
|
||||||
|
|
||||||
Here, Roger exists, and Dave is created if not existing.
|
Here, Roger exists, and Dave is created if not existing.
|
||||||
|
|
||||||
```
|
```
|
||||||
MATCH (rog :Student {name: "Roger"})
|
MATCH (rog :Student {name: "Roger"})
|
||||||
MERGE (dav :Student {name: "Dave", age:25})
|
MERGE (dav :Student {name: "Dave", age:25})
|
||||||
@ -147,8 +208,7 @@ RETURN rog, dav, type(r1), r1, type(r2), r2;
|
|||||||
|
|
||||||
Assuming Roger and Dave alredy exists, and they are connected by relationship `CLASSMATE`.
|
Assuming Roger and Dave alredy exists, and they are connected by relationship `CLASSMATE`.
|
||||||
`CREATE UNIQUE` Will create the relationship and the new node if it is not there (completely, relationship AND node). There is no relationship `FRIEND`, so it will create a new node `Dave` even though `Dave` already exists, but this time with the relationship `FRIEND`. So we have `Roger` and 2 times `Dave`, one with relation `CLASSMATE` and the other with relation `FRIEND`.
|
`CREATE UNIQUE` Will create the relationship and the new node if it is not there (completely, relationship AND node). There is no relationship `FRIEND`, so it will create a new node `Dave` even though `Dave` already exists, but this time with the relationship `FRIEND`. So we have `Roger` and 2 times `Dave`, one with relation `CLASSMATE` and the other with relation `FRIEND`.
|
||||||
```
|
|
||||||
MATCH (rog :Student {name: 'Roger'})
|
```MATCH (rog :Student {name: 'Roger'})
|
||||||
CREATE UNIQUE (rog)-[r :FRIEND]->(dav :Student {name:"Dave", age:25})
|
CREATE UNIQUE (rog)-[r :FRIEND]->(dav :Student {name:"Dave", age:25})
|
||||||
RETURN rog, type(r), dav;
|
RETURN rog, type(r), dav;```
|
||||||
```
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user