Neo4j Cypher Clauses | Neo4j Tutorial | Lecture 6

AmpCode
15 Mar 202430:35

Summary

TLDRこのビデオスクリプトでは、Neo4jデータベースでのデータ操作のためのCypher言語の基本について学びます。スクリプトは、データの読み書きに必要なCypherの様々な句(Clause)を説明し、実際のサンプルデータセットを使ってクエリの練習を通じて理解を深めます。MATCH、OPTIONAL MATCH、CREATE、DELETE、SET、REMOVEなどの主要な操作について解説され、データベースのノードやリレーションシップの追加・削除・更新方法がわかりやすく説明されています。

Takeaways

  • 📘 Neo4jは、データベース内のデータの読み書きにCypher言語を使用している。これはインタラクティブなクエリで、データの読み書きやトランザクションのコミットなどを行うための様々な節を提供している。
  • 🔍 MATCH節は、グラフデータベース内で検索するパターンを指定する基本的な機能であり、ノードとリレーションシップの構造に基づいている。
  • 👁 OPTIONAL MATCHは、特定のパターンがデータベースに存在しない場合でも、NULLを返してクエリの実行を継続できる。
  • 🌐 Neo4jデスクトップを使用して、サンプルデータセットをインポートし、Cypherシェルを開始してデータを操作することができる。
  • 🎬 データセットの例として、映画の推薦システムが使用されており、ノードとリレーションシップのタイプ(acted in, directed, genreなど)が定義されている。
  • 📊 DBMSスキーマの可視化手続きを使用することで、データベースのスキーマを理解し、グラフの構造を分析することができる。
  • 🔄 WITH節は、クエリの結果を次のクエリに渡すために使用され、不要なノードやリレーションシップをフィルタリングするのに役立つ。
  • 🛠 CREATE節を使用して、グラフに新しいノードやリレーションシップを作成することができる。
  • ❌ DELETE節は、ノードやリレーションシップを削除する際に使用され、関連するリレーションシップも明示的に削除する必要がある。
  • 🔄 UNWINDはリストを行に展開する機能で、複雑なCypherクエリでリストを操作する際に役立つ。
  • 🛂 SET節は、ノードやリレーションシップのラベルやプロパティを更新するために使用され、REMOVE節はそれらを削除する際に使用される。

Q & A

  • Neo4jとはどのようなデータベースですか?

    -Neo4jはグラフデータベースであり、ノードとリレーションシップを使ってデータをモデル化します。これはリレーショナルデータベースとは異なるデータ構造を持っており、特にネットワークや関係を中心としたデータ分析に適しています。

  • Cypher言語はなぜ必要ですか?

    -Cypher言語はNeo4jでデータを読み書きするためのインタラクティブなクエリ言語です。データの検索、書き込み、トランザクションのコミットなどを行うために使用されます。

  • MATCH節はどのような役割を持っていますか?

    -MATCH節はデータベース内で検索するパターンを指定するために使用されます。ノードとリレーションシップの構造に基づいてデータの検索条件を定義することができます。

  • OPTIONAL MATCH節とMATCH節の違いは何ですか?

    -OPTIONAL MATCH節は検索パターンがデータベースに存在しない場合でも、NULL値を返してクエリの実行を継続します。一方、MATCH節ではパターンが存在しない場合、クエリは終了します。

  • Neo4j Desktopの使い方について教えてください。

    -Neo4j Desktopはグラフデータベースの管理と操作を行うためのツールです。サンプルプロジェクトをインポートし、データベースを開始してブラウザで操作することができます。Cypherクエリを実行し、データの隠れたパターンを見つけることができます。

  • グラフデータベースのスキーマはどのように取得できますか?

    -グラフデータベースのスキーマは、DBMSの手続き呼び出しを使用して取得できます。この手続き呼び出しは、データベースのラベル、リレーションシップタイプ、およびそれらのプロパティを視覚化して理解を深めるのに役立ちます。

  • CREATE節はどのような操作を行いますか?

    -CREATE節はグラフデータベースに新しいノードやリレーションシップを作成するために使用されます。既存のノードに新しいリレーションシップを追加したり、新しいノードを作成して既存のノードに接続したりすることができます。

  • DELETE節はどのようにデータを削除するのですか?

    -DELETE節はノードやリレーションシップ、またはパスを削除するために使用されます。ノードを削除する前に、そのノードに関連するすべてのリレーションシップを明示的に削除する必要があります。

  • DETACH DELETEとDELETEの違いは何ですか?

    -DETACH DELETEはノードを削除する際に、そのノードに関連するすべてのリレーションシップも自動的に削除されます。これは単にノードを削除するDELETEとは異なり、よりクリーンなデータ削除を可能にします。

  • SET操作はどのようにデータを更新するのですか?

    -SET操作はノードやリレーションシップのラベルやプロパティを更新するために使用されます。新しいラベルを追加したり、既存のノードに新しいプロパティを設定したりすることができます。

  • REMOVE操作はどのような目的を持っていますか?

    -REMOVE操作はノードやリレーションシップからラベルやプロパティを削除するために使用されます。不要なラベルやプロパティを削除することで、データの整理や更新を行います。

  • UNWIND節はどのような役割を持っていますか?

    -UNWIND節はリストを行のシーケンスに展開するために使用されます。collectで集めた値をUNWINDで行ごとに分割し、個々の値として扱うことができます。

  • WITH節の機能は何ですか?

    -WITH節はクエリの各パートをチェーン状に繋げ、前の結果を次のクエリに渡すために使用されます。必要に応じて特定のノードやリレーションシップ、または変数のみをフィルタリングして次のステップに渡すことができます。

  • FOREACH節はどのような状況で使用されますか?

    -FOREACH節は複雑なCypherクエリで使用され、リスト内の各要素に対して操作を実行するために使われます。データのローディングや基本的なパターンの取得に役立ちます。

Outlines

00:00

😀 Neo4jとCypher言語の基本操作

この段落では、Neo4jというグラフデータベースとCypherというインタラクティブなクエリ言語について解説しています。Cypher言語を使うことで、グラフデータベース内のデータを読み書き、トランザクションをコミットすることができると説明されています。特に、データの読み込みに使用されるMatch節や、データの検索パターンを定義するOptional Match節について学ぶことができました。また、Neo4jデスクトップの使い方や、サンプルデータセットを使って実際のデータ操作を体験する手引きも紹介されています。

05:00

🎬 グラフデータベースの構造とデータの可視化

第二段落では、グラフデータベースの構造について学びます。ノードとリレーションシップがどのようにデータベースにモデル化されるかを理解することが重要です。ノードは異なるラベルで表現され、リレーションシップはそれらをつなぐもので、特定の種類を持つことができます。段落では、映画データセットを使って実際にデータベースの構造を可視化する方法を説明しており、映画のアクター、監督、ジャンル、評価などの要素がどのように関連しているかを具体的に例に挙げています。

10:01

🔍 Cypherクエリの基本構文とデータの読み方

この段落では、Cypher言語を使ってグラフデータベースを照会する方法について学びます。Match節を使ってデータベース内の特定のパターンを検索する方法や、Return節を使って検索結果を定義する方法が解説されています。また、With節を使ってクエリの結果を次のクエリに渡し、Unwind節を使ってリストを行に展開するテクニックも紹介されています。これらの節を組み合わせることで、複雑なデータ構造を効果的に照会することができると理解できます。

15:01

🛠️ グラフデータの操作: ノードとリレーションシップの作成

第四段落では、Cypher言語を使ってグラフデータベースにノードとリレーションシップを作成する方法について説明されています。Create節を使って新しいノードやリレーションシップを追加する方法を学び、方向性を持つリレーションシップの定義方法も紹介されています。また、既存のノードに関連する新しいノードやリレーションシップを作成する例も示されています。

20:03

🗑️ ノードとリレーションシップの削除

この段落では、グラフデータベースからノードやリレーションシップを削除する方法について学びます。Delete節を使って特定のノードやリレーションシップを削除する方法と、その前に関連するリレーションシップを削除する必要性について説明されています。Detach Deleteという技法を使って、ノードとそのすべての関連リレーションシップを一度に削除する方法も紹介されています。

25:04

✏️ ノードとリレーションシップのプロパティの更新

第六段落では、Set節とRemove節を使ってグラフデータベース内のノードやリレーションシップのラベルやプロパティを更新する方法について説明されています。ラベルの追加や削除、プロパティの追加や更新、そしてRemove節を使って不要なラベルやプロパティを削除する方法が具体的に紹介されています。これらの操作は、データのメンテナンスや更新に役立ちます。

30:04

📚 Cypher言語の高度な機能と総括

最後の段落では、Cypher言語の高度な機能について触れています。ForEach節やSet節、条件式、パターンマッチング、数学関数、集約関数など、データの分析や操作に役立つ多くのオプションがあることを示しています。このセクションでは、これまでに学んだ基本的な節を復習し、次に進む前にデータ操作の基礎をしっかりと身につけることが推奨されています。また、次回の講座ではこれらの高度な節について詳しく説明する予定であることが示されています。

Mindmap

Keywords

💡Neo4j

Neo4jは、グラフデータベース管理システムであり、ノード、リレーションシップ、プロパティを用いてデータの構造化を行っています。このビデオでは、Neo4jデータベースでのデータの読み書きに必要なCypher言語について学びます。例えば、ビデオではNeo4jデスクトップを使用してサンプルデータセットをインポートし、データベースの操作を開始しています。

💡Cypher言語

Cypher言語は、Neo4jで使用されるクエリ言語で、グラフデータベースに対するインタラクティブな問い合わせを行います。ビデオではCypher言語の基本的な句(Clause)について説明しており、データの読み取り、書き込み、トランザクションのコミットなどを行うための重要な役割を果たしています。

💡MATCH句

MATCH句はCypher言語で使用され、データベース内で検索するパターンを指定します。ビデオ内で例として、特定の俳優(actor)とその出演した映画(movie)の関係を検索するクエリが説明されています。MATCH句は、グラフ内のノードとリレーションシップの構造に基づいてデータを検索する際に不可欠です。

💡OPTIONAL MATCH

OPTIONAL MATCHはMATCH句の変種で、データベース内で特定のパターンが存在しない場合でもクエリの実行を継続し、欠落部分にNULLを返します。ビデオでは、存在しないパターンに対する検索でもクエリの実行を継続する重要性を強調しています。

💡CREATE句

CREATE句はCypher言語で新しいノードやリレーションシップをグラフデータベースに作成するために使用されます。ビデオでは、新しい人物(person)ノードを作成し、その人物が映画に出演する(acted in)リレーションシップを追加する例を説明しています。

💡DELETE句

DELETE句は、グラフデータベースからノードやリレーションシップを削除するために使用されます。ビデオでは、ノードの削除に必要なリレーションシップの削除について説明しており、DETACH DELETEを使用してノードとそれに関連するすべてのリレーションシップを一度に削除する方法も紹介されています。

💡SET操作

SET操作はCypher言語で使用され、ノードやリレーションシップのラベルやプロパティを更新するために使用されます。ビデオ内で例として、俳優ノードに新しいラベル(director)を追加したり、新しいプロパティ(H)を設定する操作が行われています。

💡REMOVE句

REMOVE句はSETの逆演算として、ノードやリレーションシップからラベルやプロパティを削除するために使用されます。ビデオでは、誤って追加されたラベルやプロパティを削除する例として使用されており、データの正確性を保つために重要な役割を果たしています。

💡UNWIND操作

UNWIND操作はCypher言語でリストを行のシーケンスに展開するために使用されます。ビデオでは、映画に関連するユーザーのリストを行に展開する例があります。これは複雑なCypherクエリでリストを操作する際に非常に役立つ操作です。

💡WITH句

WITH句はCypher言語でクエリの各パートをチェーン状に繋げ、前のステップの結果を次のステップで使用するために使用されます。ビデオでは、特定のノードやリレーションシップのみを次のクエリステップに渡すためにWITH句を使用する例があります。

💡プロジェクトデータセット

プロジェクトデータセットは、Neo4jデスクトップで利用可能なサンプルデータセットであり、ビデオでは推奨システムや詐欺検出システムなどの異なるタイプのデータセットをインポートして使用する方法が説明されています。これらのデータセットは、データ分析や特定のビジネスニーズに応じたグラフデータベースの操作を開始する際に役立ちます。

Highlights

Neo4j基础系列教程继续深入,介绍Cypher语言的必要性和基础用法。

Cypher语言是Neo数据库中用于读写数据的交互式查询语言。

介绍了Cypher中的不同子句,如数据读取、写入、事务提交等。

MATCH子句用于在图数据库中搜索特定模式。

OPTIONAL MATCH用于在搜索模式不存在时返回NULL,而不是结束查询。

Neo4j桌面应用程序的使用,包括如何导入数据集和启动Cypher shell。

通过Neo4j浏览器分析图数据库的结构和模式。

介绍了如何使用MATCH子句来查询特定节点和关系。

RETURN子句用于定义查询结果的输出格式。

WITH子句允许将一个查询部分的结果用作下一个查询的起点。

UNWIND操作用于将列表转换为多行数据。

CREATE子句用于在图中创建新的节点和关系。

DELETE子句用于删除图中的节点、关系或路径。

DETACH DELETE是删除节点及其所有关联关系的快捷方式。

SET操作用于更新节点和关系的标签和属性。

REMOVE操作用于从节点和关系中移除标签或属性。

介绍了如何使用Cypher进行复杂的数据操作,包括创建、删除和更新。

预告了下一讲将介绍更高级的Cypher子句,包括FOREACH、条件模式匹配等。

Transcripts

play00:00

hello and welcome back to the channel we

play00:01

are going to continue our neo4j basic

play00:04

Series in the last lecture we have seen

play00:06

all about the cipher language and why it

play00:08

is necessary for reading and writing

play00:10

data into Neo database so basically it

play00:13

is a interactive query which allow us to

play00:15

have different Clauses like reading the

play00:18

data writing the data trans committing

play00:20

the transactions and so on so basically

play00:23

we have seen all about the cipher but

play00:25

enough talking let's get our hands dirty

play00:28

and learn all of these clauses and also

play00:30

follow along with me and we'll practice

play00:32

those on our sample data set so without

play00:35

further any Ado let's get into it so the

play00:38

first Clause is reading Clauses so

play00:40

basically reading Clauses will allow you

play00:43

to read the data which is already

play00:44

present in your graph database so

play00:47

basically you can say it as a select

play00:48

clause which we usually use in the rdbms

play00:51

so basically in select Clause we will be

play00:54

having like the column names which we

play00:55

want to F from our database table so

play00:59

here as well we will be using the math

play01:01

statement and the optional math

play01:03

statement for reading the data and

play01:05

defining how your output should look

play01:07

like so here in the match Clause we will

play01:10

specify the patterns to search for in

play01:12

the database so basically graph

play01:15

databases is not structured like the

play01:17

rows and columns it is structured as per

play01:20

the nodes and relationship you have to

play01:23

Define your nodes and relationship and

play01:25

the pattern of those nodes and

play01:26

relationship so basically with that

play01:29

you'll be able to f the required data

play01:31

and in the optional match you have to

play01:33

specify the pattern to search in the

play01:35

database while also using the nulls for

play01:38

missing parts of the pattern so

play01:39

basically if the particular pattern is

play01:41

not present in your database your query

play01:43

will not return anything it will return

play01:45

as a null so basically it helps you to

play01:48

continue your query even though you

play01:49

didn't find that pattern and if you use

play01:52

math statement instead of the optional

play01:54

match that query will definitely end so

play01:57

here you have to understand the

play01:59

difference between match an optional

play02:00

match so optional match will allow you

play02:03

to continue your execution and it will

play02:05

return the nulls for missing parts of

play02:07

the pattern so let's jump on to the Neo

play02:11

4G desktop so the first so we have

play02:13

already installed NE 4G desktop on your

play02:15

PC so let's get our hands dirty and

play02:18

start the cipher shell so that we'll

play02:20

able to read the data using this match

play02:21

and optional match okay so this is our

play02:24

neo4j desktop so here as you can see in

play02:26

the beginning you will not be having any

play02:29

projects your database so here we can

play02:31

import it using the predefined data sets

play02:34

which is provided by Neo 4G itself so

play02:36

here you can click on new and here you

play02:39

can import the sample projects so there

play02:41

are different projects that are readily

play02:44

available such as the recommendation

play02:46

system the fraud detection system so

play02:48

that you can directly start working on

play02:50

the data so here we have already have

play02:53

some databases related to the

play02:54

recommendation system which we have

play02:56

imported so all you have to do is just

play02:59

start the database and click on open

play03:01

which will head you to the new forg

play03:03

browser so once we open this here as you

play03:06

can see this is where we can submit our

play03:09

Cipher queries and it is very

play03:11

interactive it is great tool for

play03:14

analyzing your data which is present in

play03:15

the graph and it allows business users

play03:19

to look into the hidden patterns in your

play03:21

data as well so to get the onology of

play03:24

the graph because you have to first

play03:26

understand the structure of the graph

play03:28

and how the notes and lab s are defined

play03:30

as well as the type of relationship

play03:33

between the two labels so to allow you

play03:35

here as you can see on the left hand

play03:38

side we got the count of notes so

play03:40

basically we have like 28k notes already

play03:42

present in our sample data set and as

play03:45

you can see we got the actor director

play03:47

genre movie person and user so basically

play03:50

it's a movie data set and by the name

play03:53

you will able to understand that it's a

play03:55

movie recommendation system and also in

play03:57

the relationship type we have like the

play03:59

acted

play04:00

directed in genre and rated

play04:02

relationships present and here are all

play04:04

the properties which are taged to the

play04:06

relationship or the nodes in your

play04:08

database but you will not understand the

play04:11

onology by just looking at this labels

play04:13

and relationships and the properties

play04:16

which are present so to get the data the

play04:18

first thing you have to do is you have

play04:20

to analyze and understand the structure

play04:22

and the onology of your graph to do that

play04:25

you just have a procedure which is

play04:27

called

play04:28

DB do schema. visualization so this

play04:32

procedure will allow you to get the

play04:34

schema of your database so as you can

play04:36

see if we expand this we have like the

play04:40

different labels here we got the

play04:42

director actor when we have the user

play04:45

which is also we have the person and the

play04:47

genre so basically we have the actor

play04:50

which can also be the person and it has

play04:52

the relationship with the movie actor

play04:55

has a relationship with movie as actor

play04:57

in so any particular actor acted in the

play05:00

movie as well as any particular actor

play05:02

can also direct the movie so it also has

play05:04

the directed relationship between these

play05:06

two notes also we have the director node

play05:09

so basically director can also act in a

play05:11

movie so that's why we have both the

play05:13

relationships tacked to this director

play05:16

and the movie and also a particular

play05:19

movie can have a genre so it is present

play05:22

in a specific genre so it is pretty

play05:24

readable graph so this is just like a

play05:26

whiteboard approach if you want to build

play05:28

your graph you already discussed about

play05:31

how to model your graph and how to

play05:33

convert the data into nodes and

play05:35

relationships you can directly start

play05:37

working on the Whiteboard and just

play05:39

writing the labels and the relationships

play05:41

and how and what will be the direction

play05:43

of the relationships between these two

play05:45

notes and also we got the user which

play05:47

basically has the rate so basically the

play05:49

particular user will rate the movie and

play05:52

also we got the person as well since

play05:55

actors are also person there could be

play05:57

like two labels assigned to a single

play05:59

record so you can relate it to the rdbms

play06:03

so if you compared it to the relational

play06:05

databases all these labels will have a

play06:07

certain table in the rdbms so we will be

play06:10

having like a actor table then the user

play06:12

table the director genre person and the

play06:15

movies table and we will be having like

play06:18

foreign keys and primary Keys between

play06:20

these tables which also represents the

play06:23

relationship types in the graph

play06:24

databases so this is how your data looks

play06:27

like and to just speak into your data

play06:29

you can just directly click on any node

play06:31

to get some sample records so here as

play06:33

you can see we will we have just click

play06:35

on the actor and we got like 25 actors

play06:38

present in a database so it will return

play06:40

some 25 records for you to just see the

play06:44

structure of the graph so here as you

play06:46

can see here we have a particular actor

play06:49

and as you can see we got the actor as

play06:52

well as person label assigned to it so

play06:54

you can say it as this particular record

play06:57

is present in both table person table

play06:59

and the ACT table if you're compared it

play07:00

to the rdbms and as you can see we have

play07:03

the actor which is born on 1879 and died

play07:07

on 1917 also it has the IMDb ID so IMDb

play07:11

is a great platform and I hope your

play07:14

movie nerds will already know about it

play07:16

so it is a Eric Campbell so basically

play07:19

this particular actor also present as a

play07:22

person label if you double click this

play07:24

this will expand it and gives us the

play07:27

nodes which are connected to this act

play07:29

actor so as you can see this actor acted

play07:32

in a film which is called as immigrant

play07:35

so this Immigrant movie came in 1970 so

play07:37

these are like all the properties which

play07:39

are present on each node and here as you

play07:42

can see in the relationship we also have

play07:44

the role that particular user has played

play07:47

in which is like the head waiter so this

play07:50

head waiter role is played by the Eric

play07:52

Campbell in the Immigrant movie which

play07:55

came in 1970 so this is pretty

play07:57

self-explanatory and also there is

play08:00

another actor which played in The

play08:01

Immigrant movie which is the Anna pan I

play08:04

don't know how to pronounce that name

play08:06

but this is what it is and also she

play08:09

played a role as a immigrant in this

play08:11

movie and also we have the Albert Al

play08:13

Austin which played the diner and the

play08:15

Immigrant role in this movie so this

play08:18

graph is pretty self-explanatory but

play08:20

this is not the point we have to learn

play08:21

about all the Clauses of Cipher and how

play08:24

to read the data using the cipher so

play08:27

first one is as you can see we got the

play08:29

match so match will basically allow you

play08:32

to specify the patterns which you have

play08:34

to search in your database so as you can

play08:37

see the pattern means you have this

play08:39

pattern we have the two nodes which are

play08:41

connected using the acted in

play08:43

relationship so to represent it we have

play08:45

already seen how to represent a node as

play08:48

well as the relationship in the cipher

play08:50

so we will directly go over to the

play08:53

command line and start writing our

play08:55

Cipher query so here the first step will

play08:57

be the match Clause then we have to

play08:59

specify the nodes which is like the

play09:02

starting point of our pattern so here

play09:04

I'll assign a variable to our node which

play09:06

is a and it should be actor so basically

play09:10

this is the actor so if you want to just

play09:13

get this particular actor as well as the

play09:15

Immigrant you have to just search for a

play09:18

property in that actor label so here we

play09:21

can directly denote it in the curly

play09:23

brackets and we have the name of that

play09:26

actor so we can have like the name here

play09:29

then we give the colon and in quotes

play09:32

which is like a text format we can give

play09:34

the Eric Campbell name here so right now

play09:36

we just got the particular actor and we

play09:38

hold it in the a variable and as you can

play09:41

see we got the acted in relationship

play09:43

which is going from actor to the movie

play09:46

so here we can denote it in the square

play09:48

brackets and the relationship type is

play09:50

acted in so we have the acted in and the

play09:53

acted in is further connected so as you

play09:56

can see this is directed from the actor

play09:58

node to the movie node and here we will

play10:00

give our movie node so I'll assign it a

play10:03

m variable and here is our movie so

play10:07

basically this will give us all the

play10:08

movies which are related to Eric

play10:10

Campbell and again we can return this

play10:13

pattern so our query will look out for

play10:16

this pattern in a whole graph and give

play10:17

us the result so the return Clause is

play10:20

used to define how your output looks

play10:22

like we are going to also see the return

play10:24

clause in the subsequent topic but let's

play10:26

say we'll just return all here

play10:29

so as you can see we got the required

play10:32

pattern we got the Eric Campbell and the

play10:34

movie ID so this is how the match Clause

play10:36

looks like so let's say if the

play10:38

particular pattern is not present in the

play10:40

database so what we can do is we can

play10:43

just give like the directed in because

play10:47

Chris Campbell didn't actually directed

play10:49

in any movie right so we know that this

play10:52

pattern doesn't exist let's say we will

play10:54

return all here so if we return all here

play10:58

you can see there is no output to this

play11:00

query so if we use the optional match it

play11:03

will look out for the pattern and it

play11:05

will instead return the null instead of

play11:07

returning nothing so this is very useful

play11:10

if you have like long Cipher queries and

play11:12

you have to check for any conditions

play11:14

which and patterns which are present in

play11:16

the database so if we just give optional

play11:20

before the math statement then let's see

play11:23

what happens so as you can see we got

play11:26

the null value here so it will help you

play11:29

to

play11:29

continue the execution even though that

play11:32

pattern is not available in the database

play11:34

this is the basic difference between

play11:36

match and the optional match and many of

play11:39

the developers will struggle using this

play11:42

optional match in the query because

play11:44

optional match is pretty expensive in

play11:46

some scenarios so you have to make sure

play11:49

that you're understanding the difference

play11:50

between match and optional match

play11:52

carefully this is all about reading

play11:54

Clauses let's talk about the projecting

play11:57

Clauses now okay so the next one is the

play11:59

projecting Clauses so as you can see

play12:01

this will help us to project our graph

play12:04

and how our end graph will really look

play12:06

like so basically you can also get the

play12:09

tabular output as well as the graphical

play12:12

output which we just seen so graphical

play12:14

output will return you the nodes and

play12:16

relationships and the tabular output

play12:19

will give you all the properties which

play12:21

are present in a node or a relationship

play12:24

so as you can see the first Clause we

play12:26

have already seen before 1 minute so

play12:29

return Clause so return Clause will

play12:31

Define what to include in the query

play12:33

result set so it will be either a

play12:35

tabular form or it will be bunch of noes

play12:38

and relationships so we have already

play12:41

seen it so as you can see we are

play12:43

returning the notes here but let's say

play12:46

if you want to just display the name of

play12:49

that actor as well as the birth date of

play12:52

that actor that also we can do so we can

play12:54

use the return Clause so let's say we

play12:57

have to return the name of that actor

play12:59

so we got the name in the name property

play13:02

here so we will give like a do name

play13:05

because a represents the variable of

play13:08

actor and also we have like the a do BN

play13:12

here which is like a birth date for that

play13:15

particular actor so if you return it as

play13:18

you can see we got a tabular output as

play13:20

the name and bond date of that

play13:22

particular actor this is very simple so

play13:25

the next Clause is with Clause so as you

play13:28

can see with Clause will allow the query

play13:30

parts to be chained together so this

play13:33

will also help you to piping the result

play13:35

from one to be use as a starting point

play13:38

or the criteria to the next so let's say

play13:41

if you have a longer query here so let's

play13:43

say if you have a longer query you got

play13:46

like 20 to 30 lines of query and your

play13:49

start statement will have like some

play13:51

patterns which includes 10 15 nodes or

play13:54

and 5 to 10 relationships but you don't

play13:56

need that subsequently and those will be

play14:00

reducing in each step so as you can see

play14:03

here we are only returning the name and

play14:05

the bond which is only present in the

play14:07

actor node we're not even utilizing the

play14:10

movie node so why we should return that

play14:12

and keep that in the last return

play14:14

statement so before that we can add like

play14:17

width Clause here so let's say we have

play14:19

the width clause and we are only needing

play14:21

the actor node here so it filters out

play14:24

the movie node and it will not be used

play14:27

or we cannot use it in the subsequent

play14:29

statements so if you just return return

play14:31

this the query will not be hampered but

play14:34

if you just get only the movie then this

play14:38

will give you the error that variable a

play14:40

is not defined because that variable we

play14:42

are not passing it using the with Clause

play14:45

so with Clause will really help you to

play14:47

pass only the required variables or the

play14:50

nodes or maybe relationships in the next

play14:53

steps so let's discuss that with the

play14:55

example which will really make sense

play14:57

because this is not making much sense

play14:59

and you will not able to understand it

play15:01

correctly so let's say if you have the

play15:03

actor node and the movie node then we

play15:06

have another operation on the movie node

play15:09

so here let's say we have to just then

play15:12

match on this movie so we are just using

play15:15

the math statement again and here we can

play15:18

maybe remove this and just give it star

play15:22

so here we are going to have like all

play15:25

the nodes which are connected further to

play15:27

this movie so we have all the

play15:29

relationships so we can denote like Star

play15:32

here and also here we can denote just

play15:35

empty or maybe give some variable which

play15:37

is like just node two right so here we

play15:41

are getting all the nodes which are

play15:43

connected to that particular movie so

play15:45

here we are not at all using our actor

play15:48

node so why we should keep that so to

play15:51

only get the M node which is movie node

play15:54

we can just directly filter it out using

play15:57

the width M so

play15:59

only you can use M variable which is

play16:02

movie in the subsequent step where we

play16:04

are matching it matching all the notes

play16:07

and returning it in the last return

play16:10

Clause so as you can see we got all the

play16:12

connected notes here to that particular

play16:15

movie so here if we just expand this we

play16:18

got like the movie here we got like the

play16:21

user then we have like another user

play16:23

which rated this movie also we have like

play16:26

the actor as well as the Director of

play16:28

this movie and and all other three

play16:30

actors and also Eric Amell is also

play16:34

present here but we are not passing it

play16:36

using the with Clause we are only

play16:37

limiting it so with Clause will allow

play16:40

you to just filter out and only pass the

play16:43

certain information which is required

play16:45

into the subsequent processes so this is

play16:48

how you can use the with clause in the

play16:50

cipher let's talk about the another

play16:52

Clause now so next we have the unwind

play16:55

here so unwind is basically expand list

play16:58

into to the sequence of rows so it is

play17:01

very easy to understand so here like

play17:03

collect is used for converting some

play17:06

nodes into into list and here we can use

play17:10

unwind to just expand that list into a

play17:13

sub sequence of rows so let's talk about

play17:15

the unwind now so here let's say let's

play17:19

create a list here so we have like two

play17:21

users which are which are connected to

play17:24

this movie so let's say we have like the

play17:28

user here so no two will have the user

play17:32

and if we just return this we got the

play17:34

two user along with the movie so let's

play17:37

say if we collect those users and just

play17:40

collect note two as the nodes here and

play17:44

let's return the nodes now so here we

play17:46

got the nodes but collect will only make

play17:49

sense when we are passing some

play17:51

properties inside it so let's say if we

play17:53

just pass the name of those users so

play17:56

let's say we got the note 2. name name

play17:59

here and once we return the names here

play18:02

so let's re rename it as

play18:04

names and return it as you can see we

play18:08

got the list of the users that

play18:10

particular movie has submitted ratings

play18:12

to so basically we got the list but

play18:15

let's say if you want to explore this

play18:16

list and convert it into number of rows

play18:19

then we can use the unwind operation

play18:21

here so basically we have the list named

play18:24

as names so in the next step let's

play18:27

unwind those names here so let's unwind

play18:30

it so we will unwind the names as like

play18:34

the rows here and we will return the

play18:37

rows now and if you return it as you can

play18:41

see our components has converted into

play18:43

the rows so you will say what is the

play18:46

actual use of unwind here we are going

play18:50

to see in the subsequent lectures where

play18:52

we will see how to utilize this collect

play18:54

and unwind operation in the complex

play18:56

Cipher queries but for your basic

play18:59

understanding collect will convert some

play19:01

values into a list and the unwind will

play19:04

unwind that list and convert it into the

play19:07

individual values which you can return

play19:09

it in different rows so this is very

play19:12

simple to understand so this was all

play19:14

about the projecting Clauses let's head

play19:17

over to our next clause which is also

play19:19

very important and used while

play19:21

manipulating the data so now let's talk

play19:24

about the writing Clauses here so here

play19:27

we have like different WR writing

play19:29

Clauses which you can use to manipulate

play19:31

or create or delete the nodes in your

play19:34

database so as you can see the first one

play19:36

is the create so as the name suggest it

play19:38

used to create the nodes and

play19:40

relationships in your graph so let's

play19:43

jump over to how to use a create Clause

play19:45

so let's say if you want to create a

play19:47

node in your graph so here we will use

play19:49

the create clause and we will just

play19:52

create another node so let's say we have

play19:54

to create a person here so here we have

play19:58

created a person so to create a person

play20:00

you have to assign a particular label to

play20:02

it it is very important because every

play20:05

record will have some label in your

play20:07

graph and you have to define the some

play20:10

properties to that person so let's say

play20:12

we have the name here then we have to

play20:15

give the name so name will be let's say

play20:18

I'll give my name and here our next is

play20:22

like we have like some Edge so I'll be

play20:25

having some ede here so this will create

play20:28

a person node which has the label person

play20:31

and also has the properties like name

play20:34

and the age so this is how you can

play20:37

create a person label and let's just

play20:39

return it and see how it creates the

play20:41

note so we'll return the a so as you can

play20:45

see the node is created in our database

play20:48

which has the label person which has the

play20:50

age and the name so let's say and if you

play20:53

want to create a relationship with this

play20:55

node so if this person has acted in some

play20:58

movie let's say so to to just do that

play21:02

instead of create let's say we will just

play21:04

match it so match will match that part

play21:07

find that particular pattern which is

play21:09

nothing but this particular node in our

play21:12

database and after that we can just

play21:15

create another node so here we will

play21:17

create another node which is like the

play21:20

movie here right and the movie will have

play21:23

some certain name here so the name is

play21:26

like the Hulk maybe for example so here

play21:29

we also created a node here and to

play21:32

create relationship we can also use like

play21:35

a create Clause here so we can again use

play21:37

the create clause and to define a

play21:39

relationship we already have our

play21:41

variables which is a which is tag to a

play21:43

person which we just created and also we

play21:46

have the movie which we are going to

play21:47

create in this query so here a then we

play21:51

got the relationship so relationship

play21:53

type would be like acted in and then we

play21:56

can give our newly created node which we

play21:59

are going to create in this execution

play22:00

which is M this is very simple but this

play22:03

will throw an error because we don't

play22:05

have any direction to our relationship

play22:07

we should have some direction to our

play22:10

relationship so here it will make sense

play22:13

that person should act in a movie so the

play22:16

direction will go from left to right so

play22:19

just give that and we will return return

play22:21

all the notes which is a comma M that's

play22:24

it if you run this it will create the

play22:26

movie node also it will create the actor

play22:29

relationship between the person and the

play22:31

movie node just return it and as you can

play22:33

see we got the person node which was

play22:36

already created so we have just grab

play22:38

that using the match so this will look

play22:40

out for this pattern which is in case

play22:42

this person node and we got our movie

play22:44

here which is the Hulk this was very

play22:47

simple let's jump on to the next Clause

play22:49

here so the next Clause is like delete

play22:52

so delete basically as the name suggest

play22:54

deletes the nodes or relationships or

play22:57

the paths so any note to be deleted must

play22:59

also have like Associated relationship

play23:02

which has explicitly deleted so what it

play23:05

means is before you can delete a node

play23:08

you have to just delete all the

play23:10

relationships which is assigned to it

play23:12

otherwise it will throw an error let's

play23:14

do that in our example so as you can see

play23:17

we have already created the movie and

play23:19

our user so let's just get this person

play23:22

now and let's try to delete it so to

play23:25

delete it we will just match that and

play23:28

get the pattern which is nothing but the

play23:29

node and we will try to delete this node

play23:32

and let's see what happens if you

play23:34

execute it as you can see we cannot

play23:37

delete the node because it still has the

play23:39

relationships so to delete this node you

play23:41

must first delete its relationship so to

play23:44

delete a relationship you just have to

play23:46

grab this acted in and then delete it so

play23:50

to delete a particular relationship you

play23:52

have to write a match clause and then

play23:54

you have to just go ahead and grab the

play23:57

relationship which is taged to it and

play23:59

then you have to delete the r here so

play24:02

here you will able to delete this R and

play24:05

then you can able to delete the note so

play24:06

if you execute it as you can see we have

play24:09

got like deleted one relationship here

play24:12

and once you get this person and try to

play24:14

delete it it will delete it right away

play24:17

so if you delete this as you can see it

play24:20

is giving as deleted one node so this is

play24:23

how you can do that but there is another

play24:25

work around or there is another way to

play24:28

delete a particular node from your graph

play24:30

which is that detach delete so with

play24:33

detach delete you can delete a node or

play24:36

set of notes and all the associated

play24:38

relationships will automatically be

play24:40

deleted this is pretty simple so here

play24:43

let's say we got the actor node here and

play24:46

let's jump on and get our IR Campbell

play24:49

again so we will just match our person

play24:52

so this node is not present and we will

play24:55

just hold on to our Eric Campbell here

play24:58

so we'll just give the Eric Campbell

play25:01

don't have any H and let's return this

play25:04

so if you return this node it has some

play25:06

movie assigned to it so it has some

play25:09

relationship and node connected to a

play25:11

Campell node so if we just directly try

play25:14

to delete it it will not allow us

play25:16

because it still has the relationship

play25:18

but we if use detach delete it will

play25:21

delete the relationship including the

play25:23

node which is nothing but the person

play25:24

node so just detach and delete a and

play25:29

there you go it deleted the node as well

play25:31

as it deleted the relationship which we

play25:33

got in the result here so this is how

play25:36

you can use the create clause and delete

play25:38

Clause to create and delete the nodes in

play25:40

your graph database so the next one is

play25:42

set operation so set operation is

play25:45

nothing but updates the label on the

play25:47

nodes as well as the properties on the

play25:49

nodes and relationship so this is pretty

play25:51

simple the set can add another label to

play25:54

your node so if you have an actor node

play25:56

which has like actor label and if you

play25:57

want add like user label to it you can

play26:00

easily do that using set and if you in

play26:02

your actor if you want to add like

play26:04

another property in that node you can

play26:06

also use it using set let's do that now

play26:09

so as you can see we got the Eric

play26:11

Campbell here and it got deleted so

play26:14

let's pick another actor here so we have

play26:16

like the Anna Province here I don't know

play26:19

the pronunciation of this so I'll just

play26:21

paste it over here so as you can see we

play26:24

got this particular person so we'll just

play26:27

return the person a so let's say this

play26:31

particular person has like two labels

play26:34

actor and person but we also have to add

play26:37

another label to it so our label could

play26:39

be like director maybe so we can do that

play26:42

using set operation so you have to just

play26:44

use it between match and return so just

play26:47

use set operation we'll get the variable

play26:49

first that is the first step then do the

play26:52

colon and you can add the next label so

play26:55

here we will add like director here and

play26:58

we will just return that node so if you

play27:00

execute it as you can see we got the

play27:02

director label added to our person node

play27:05

or the actor node so here we can also

play27:08

add another property let's say we have

play27:11

to add like H property to it so we can

play27:14

do that using the set operation so here

play27:16

to just set some property you have to

play27:19

you don't have to use colon you have to

play27:21

use dot here so you can you give like

play27:23

set a DOT and the property name so we

play27:26

can give any property name here we can

play27:28

maybe give like H and the H could be

play27:31

maybe 30 right and if we return this it

play27:35

will have the H mistakenly I have just

play27:37

given the 300 here so I'll just rerun it

play27:41

as you can see the H property is added

play27:43

and the value is 30 this is so simple

play27:46

and opposite to our set Clause there is

play27:49

a remove Clause so remove Clause will be

play27:51

used for removing a certain label or a

play27:55

property from your node or the

play27:57

relationship so for relationship it will

play27:59

go same way you just have to assign a

play28:02

variable to your relationships like

play28:03

maybe you can give R we can just set

play28:05

some property to it which is like R do H

play28:09

is equal to 30 this is so simple as that

play28:12

and remove will is the negation of the

play28:14

set which removes any label or the

play28:17

property from this note so let's remove

play28:19

that director label and the H property

play28:21

here so just instead of set just use

play28:24

remove

play28:25

here and you don't have to just give

play28:28

equal to 30 because it already has the

play28:30

value so this will remove the H property

play28:33

from our person node or the actor node

play28:36

so as you can see if you go into this

play28:39

yeah the age is no longer there and also

play28:42

just let's remove the director label as

play28:44

well so we'll remove a here you have to

play28:46

remember again you have to give like

play28:48

colon because for for defining the label

play28:51

you have to give colon and for defining

play28:54

a property have to mention the dot here

play28:56

so a

play28:58

director and we will just return it

play29:00

again and as you can see the director

play29:03

label has been removed from our person

play29:06

and actor note so this is how you can

play29:08

easily remove the any property or label

play29:11

in your graph and this will help you for

play29:13

daily ingesting your nodes and

play29:16

relationships or maybe you can say the

play29:18

whole data into your graph so this is

play29:20

like a 4 Clause so for each we update

play29:23

the data within a list so whether it

play29:25

could be like component of path or the

play29:28

result of the aggregation so this is

play29:30

part of complex Cipher so let's focus on

play29:33

the basics first because we will not be

play29:35

using for each very often while loading

play29:38

the data it is only useful while loading

play29:41

the data or getting the basic patterns

play29:43

from your database so for easy

play29:46

manipulation Reading Writing everything

play29:49

this Clauses which we learned are very

play29:51

much enough for you but in the next

play29:53

lecture let's talk about some complex

play29:55

Clauses like for each clauses also we

play29:58

have the set Clauses we also have like

play30:01

wear conditions pattern matching etc etc

play30:04

there are so many options there are

play30:06

different functions like mathematical

play30:08

functions then we have the aggregation

play30:10

functions to able to aggregate the

play30:12

output and get the meaning out of your

play30:15

data so this could be really fun but

play30:18

let's stop here and in the next lecture

play30:20

let's talk about the advanced Cipher

play30:23

Clauses so I hope you understood this so

play30:25

if you have any difficulties you can let

play30:27

me know in the comments and we can talk

play30:29

about it further that's it for today

play30:31

I'll see you in the next lecture thanks

play30:33

for watching

Rate This

5.0 / 5 (0 votes)

Do you need a summary in English?