From da0b7518db5d85334935733ad4407ceab556902f Mon Sep 17 00:00:00 2001 From: Irina Loghin Date: Tue, 16 Dec 2025 13:58:48 +0000 Subject: [PATCH] added slides mode --- .../courses/cypher-fundamentals/course.adoc | 1 + .../lessons/1-intro-cypher/lesson.adoc | 38 ++++++++++++-- .../lessons/3-relationships/lesson.adoc | 12 +++++ .../lessons/6-filtering-queries/lesson.adoc | 31 ++++++++---- .../modules/1-reading/module.adoc | 7 +++ .../lessons/1-create-nodes/lesson.adoc | 15 ++++-- .../3-create-relationships/lesson.adoc | 30 +++++++++--- .../lessons/5-updating-properties/lesson.adoc | 40 +++++++++------ .../lessons/7-merge-processing/lesson.adoc | 36 +++++++++++--- .../2-writing/lessons/9-delete/lesson.adoc | 49 ++++++++++++++----- .../modules/2-writing/module.adoc | 7 +++ 11 files changed, 209 insertions(+), 57 deletions(-) diff --git a/asciidoc/courses/cypher-fundamentals/course.adoc b/asciidoc/courses/cypher-fundamentals/course.adoc index 05a8052ec..f16ad7de3 100644 --- a/asciidoc/courses/cypher-fundamentals/course.adoc +++ b/asciidoc/courses/cypher-fundamentals/course.adoc @@ -8,6 +8,7 @@ :video: https://www.youtube.com/embed/Se_Zwiew90Q :translations: jp-cypher-fundamentals,cn-cypher-fundamentals :key-points: Cypher patterns, Reading data from a graph, Writing data to a graph +:slides: true //https://youtu.be/Se_Zwiew90Q diff --git a/asciidoc/courses/cypher-fundamentals/modules/1-reading/lessons/1-intro-cypher/lesson.adoc b/asciidoc/courses/cypher-fundamentals/modules/1-reading/lessons/1-intro-cypher/lesson.adoc index aa2c3a568..98d196f0a 100644 --- a/asciidoc/courses/cypher-fundamentals/modules/1-reading/lessons/1-intro-cypher/lesson.adoc +++ b/asciidoc/courses/cypher-fundamentals/modules/1-reading/lessons/1-intro-cypher/lesson.adoc @@ -1,6 +1,7 @@ = Introduction to Cypher :type: video :order: 1 +:slides: true [.video] video::jEIE_b1MzAE[youtube,width=560,height=315] @@ -22,6 +23,7 @@ https://docs.google.com/document/d/1u5OO6OOUnD0JVdnsGjj5Zs1DLPzUiqFKVO7W4tmBh9k/ // so that I can *find out more about him* [.transcript] +[.slide.discrete] == What is Cypher? Cypher is a query language designed for graphs. @@ -31,6 +33,9 @@ In this example, the entities are people and movies. We have _Person_ and _Movie image::images/whiteboard.jpg[Whiteboard-image,width=600,align=center] +[.slide] +== Cypher Pattern Syntax + Just as we would draw circles and arrows on a whiteboard, we write out the pattern in Cypher: * Nodes are represented by parentheses `()`. @@ -41,6 +46,9 @@ Just as we would draw circles and arrows on a whiteboard, we write out the patte * Properties drawn in a _speech bubble_ are specified in a JSON like syntax. ** Properties in Neo4j are key/value pairs, for example `{name: 'Tom Hanks'}`. +[.slide] +== Example Cypher Pattern + For example, a Cypher pattern in the graph could be: [source,Partial,role=nocopy noplay] @@ -55,7 +63,8 @@ The specific _Movie_ node in this pattern is filtered by the _title_ property wi So this pattern represents all people in the graph who acted in the movie, _Cloud Atlas_. -=== How Cypher works +[.slide] +== How Cypher works Cypher works by matching patterns in the data. We retrieve data from the graph using the `MATCH` keyword. @@ -69,15 +78,14 @@ MATCH (:Person) // incomplete MATCH clause because we need to return something ---- +[.slide] +== Returning Results with Variables + Suppose we want to retrieve all _Person_ nodes from the graph. We can assign a variable by placing a value before the colon. Let's use the variable `p`. Now that `p` represents all _Person_ nodes retrieved from the graph, we can return them using the `RETURN` clause. Run this Cypher code: -[TIP] -.Running Queries in Sandbox -Click the *Run in Sandbox* button to the top right of the code sample to open the Sandbox to the right and run the query. - [source,cypher] ---- MATCH (p:Person) @@ -88,6 +96,14 @@ This query returns all nodes in the graph with the _Person_ label. You can view the results returned using the graph view or the table view. When you select the table view, you can also see the properties for the nodes returned. +[TIP] +.Running Queries in Sandbox +Click the *Run in Sandbox* button to the top right of the code sample to open the Sandbox to the right and run the query. + + +[.slide] +== Filtering by Property + Now, say we want to find the node which represents the _Person_ who's name is _Tom Hanks_. Our _Person_ nodes all have a _name_ property. We can use the __braces__ `{..}` to specify the key/value pair of _name_ and _Tom Hanks_ as the filter. @@ -103,6 +119,9 @@ This query returns a single node that represents _Tom Hanks_. In the graph view of Neo4j Browser, the node is visualized as a bubble. You can also view the results returned in table view where you can view the properties of the node. +[.slide] +== Accessing Properties + In our Cypher statement, we can access properties using a _dot notation_. For example, to return the _name_ property value using its property key `p.name`. @@ -115,6 +134,9 @@ RETURN p.born This query returns the value of the _born_ property of the _Tom Hanks_ node. +[.slide] +== Case Sensitivity + [IMPORTANT] .Understanding Case Sensitivity -- @@ -128,6 +150,9 @@ Neo4j best practices include: * Use **UPPERCASE** for Cypher keywords. -- +[.slide] +== Using the WHERE Clause + Another way that you can filter queries is by using the `WHERE` clause, rather than specifying the property value inline with braces. This query returns the same data as the previous query. @@ -139,6 +164,9 @@ WHERE p.name = 'Tom Hanks' RETURN p.born ---- +[.slide] +== Adding more logic to your queries + As you gain more experience with Cypher, you will find that using `WHERE` to filter your queries is very powerful because you can add more logic to your `WHERE` clause. Here is an example where we filter by two values for _name_. diff --git a/asciidoc/courses/cypher-fundamentals/modules/1-reading/lessons/3-relationships/lesson.adoc b/asciidoc/courses/cypher-fundamentals/modules/1-reading/lessons/3-relationships/lesson.adoc index 65c1e4a4c..bea480bce 100644 --- a/asciidoc/courses/cypher-fundamentals/modules/1-reading/lessons/3-relationships/lesson.adoc +++ b/asciidoc/courses/cypher-fundamentals/modules/1-reading/lessons/3-relationships/lesson.adoc @@ -1,6 +1,7 @@ = Finding Relationships :type: video :order: 3 +:slides: true [.video] video::aQ5hKd2DD0A[youtube,width=560,height=315] @@ -17,6 +18,7 @@ https://docs.google.com/document/d/1EGqqe-05pSBVL5TTkRv_MoSkkhqQQBSjyS7pv9URyKE/ [.transcript] +[.slide.discrete] == Finding relationships [TIP] .Our Goal @@ -32,6 +34,10 @@ In the previous lesson, we used the `MATCH` clause to find the node in our datab MATCH (p:Person {name: 'Tom Hanks'}) RETURN p ---- + +[.slide] +== Traversing Relationships + We can extend the pattern in the `MATCH` clause to _traverse_ through all relationships with a type of _ACTED_IN_ to any node. Our domain model shows that the _ACTED_IN_ relationship goes in an outgoing direction from the _Person_ node so we can add the direction in our pattern. We often refer to this as a *traversal*. @@ -42,6 +48,9 @@ We often refer to this as a *traversal*. MATCH (p:Person {name: 'Tom Hanks'})-[:ACTED_IN]->() ---- +[.slide] +== Returning Related Nodes + Our data model dictates that the node at the other end of that relationship will be _Movie_ node, so we don't necessarily need to specify the _:Movie_ label in the node - instead we will use the variable _m_. [source,cypher] @@ -52,6 +61,9 @@ RETURN m.title This code returns the titles of all movies that _Tom Hanks_ acted in. +[.slide] +== Specifying Node Labels + If our graph had different labels, for example Television and _Movie_ nodes this query would have returned all _Television_ and _Movie_ nodes that Tom Hanks acted in. That is, if we had multiple _types_ of nodes at the end of the _ACTED_IN_ relationships in our graph, we could make sure that we only return movies. diff --git a/asciidoc/courses/cypher-fundamentals/modules/1-reading/lessons/6-filtering-queries/lesson.adoc b/asciidoc/courses/cypher-fundamentals/modules/1-reading/lessons/6-filtering-queries/lesson.adoc index 1567bcfb4..693b7de4b 100644 --- a/asciidoc/courses/cypher-fundamentals/modules/1-reading/lessons/6-filtering-queries/lesson.adoc +++ b/asciidoc/courses/cypher-fundamentals/modules/1-reading/lessons/6-filtering-queries/lesson.adoc @@ -1,6 +1,7 @@ = Filtering Queries :type: video :order: 6 +:slides: true [.video] video::ZBbH5p-5Gt8[youtube,width=560,height=315] @@ -18,6 +19,7 @@ https://docs.google.com/document/d/1mZ6afZ6CfN0XADSc80NQS3kXxFN9hT_5qAYVVUeKr5c/ [.transcript] +[.slide.discrete] == Filtering queries Earlier, you learned that the `WHERE` clause is used to tell the query engine to filter what nodes are retrieved from the graph. @@ -34,7 +36,8 @@ WHERE m.released = 2008 OR m.released = 2009 RETURN p, m ---- -=== Filtering by node labels +[.slide] +== Filtering by node labels You have already seen this type of query. It returns the names of all people who acted in the movie, _The Matrix_. @@ -56,7 +59,8 @@ RETURN p.name Both queries execute the same way, but you may want to use one style of filtering over another in your code. -=== Filtering using ranges +[.slide] +== Filtering using ranges You can specify a range for filtering a query. Here we want to retrieve _Person_ nodes of people who acted in movies released between _2000_ and _2003_: @@ -68,7 +72,8 @@ WHERE 2000 <= m.released <= 2003 RETURN p.name, m.title, m.released ---- -=== Filtering by existence of a property +[.slide] +== Filtering by existence of a property Recall that by default, there is no requirement that a node or relationship has a given property. Here is an example of a query where we only want to return _Movie_ nodes where _Jack Nicholson_ acted in the movie, and the movie has the _tagline_ property. @@ -80,7 +85,8 @@ WHERE p.name='Jack Nicholson' AND m.tagline IS NOT NULL RETURN m.title, m.tagline ---- -=== Filtering by partial strings +[.slide] +== Filtering by partial strings Cypher has a set of string-related keywords that you can use in your `WHERE` clauses to test string property values. You can specify `STARTS WITH`, `ENDS WITH`, and `CONTAINS`. @@ -104,7 +110,8 @@ WHERE toLower(p.name) STARTS WITH 'michael' RETURN p.name ---- -=== Filtering by patterns in the graph +[.slide] +== Filtering by patterns in the graph Suppose you wanted to find all people who wrote a movie but did not direct that same movie. Here is how you would perform the query: @@ -116,15 +123,14 @@ WHERE NOT exists( (p)-[:DIRECTED]->(m) ) RETURN p.name, m.title ---- -=== Filtering using lists +[.slide] +== Filtering using lists If you have a set of values you want to test with, you can place them in a list or you can test with an existing list in the graph. A Cypher list is a comma-separated set of values within square brackets. You can define the list in the `WHERE` clause. During the query, the graph engine will compare each property with the values `IN` the list. -You can place either numeric or string values in the list, but typically, elements of the list are of the same type of data. -If you are testing with a property of a string type, then all the elements of the list will be strings. In this example, we only want to retrieve _Person_ nodes of people born in _1965_, _1970_, or _1975_: @@ -135,6 +141,9 @@ WHERE p.born IN [1965, 1970, 1975] RETURN p.name, p.born ---- +[.slide] +== Filtering with existing lists in the graph + You can also compare a value to an existing list in the graph. We know that the _:ACTED_IN_ relationship has a property, _roles_ that contains the list of roles an actor had in a particular movie they acted in. @@ -147,7 +156,8 @@ WHERE 'Neo' IN r.roles AND m.title='The Matrix' RETURN p.name, r.roles ---- -=== What properties does a node or relationship have? +[.slide] +== What properties does a node or relationship have? The properties for a node with a given label need not be the same. One way you can discover the properties for a node is to use the `keys()` function. @@ -164,7 +174,8 @@ RETURN p.name, keys(p) The results returned for each row include the name of the person, followed by the list of property keys for that node. If you scroll down in the result pane, you will notice that some Person nodes do not have a born property. -=== What properties exist in the graph? +[.slide] +== What properties exist in the graph? More generally, you can run this code to return all the property keys defined in the graph. diff --git a/asciidoc/courses/cypher-fundamentals/modules/1-reading/module.adoc b/asciidoc/courses/cypher-fundamentals/modules/1-reading/module.adoc index a56598ca8..5f8e91c2a 100644 --- a/asciidoc/courses/cypher-fundamentals/modules/1-reading/module.adoc +++ b/asciidoc/courses/cypher-fundamentals/modules/1-reading/module.adoc @@ -1,9 +1,15 @@ = Reading Data from Neo4j :order: 1 +:slides: true +[.slide.discrete] +== Introduction In this module you will learn how to write Cypher code to retrieve data from the graph. +[.slide] +== What You Will Learn + You will learn how to: * Retrieve nodes from the graph. @@ -15,6 +21,7 @@ You will learn how to: Using the Movies example dataset, you will create and execute Cypher code to find actors and movies in our graph. +[.slide] == Domain model for this course Here is the data model used in this course. diff --git a/asciidoc/courses/cypher-fundamentals/modules/2-writing/lessons/1-create-nodes/lesson.adoc b/asciidoc/courses/cypher-fundamentals/modules/2-writing/lessons/1-create-nodes/lesson.adoc index 2e0a8ffe5..82dd0bb12 100644 --- a/asciidoc/courses/cypher-fundamentals/modules/2-writing/lessons/1-create-nodes/lesson.adoc +++ b/asciidoc/courses/cypher-fundamentals/modules/2-writing/lessons/1-create-nodes/lesson.adoc @@ -1,16 +1,20 @@ = Creating Nodes :type: video :order: 1 +:slides: true [.video] video::fgoz4eMQHOk[youtube,width=560,height=315] + +[.transcript] +[.slide.discrete] +== Creating nodes + [NOTE] .Video Transcript Correction The actor's name for Michael Caine is misspelled in the video. It has been corrected in the transcript. -[.transcript] -== Creating nodes In this lesson you will learn how to write Cypher code to create nodes in the graph. Using the Movies data model, you will create and execute Cypher code to create actors and movies in our graph. @@ -20,6 +24,9 @@ We use the `MERGE` keyword to create a _pattern_ in the database. After the `MERGE` keyword, we specify the pattern that we want to create. Usually this will be a single node or a relationship between two nodes. +[.slide] +== Creating a node + Suppose we want to create a node to represent _Michael Caine_. Run this Cypher code to create the node. @@ -41,7 +48,8 @@ RETURN p // browser::MATCH (n) RETURN count(n)[] -=== Executing multiple Cypher clauses +[.slide] +== Executing multiple Cypher clauses We can also chain multiple `MERGE` clauses together within a single Cypher code block. @@ -55,6 +63,7 @@ RETURN p, m This code creates two nodes, each with a primary key property. Because we have specified the variables _p_ and _m_, we can use them in the code to return the created nodes. +[.slide] == Using `CREATE` instead of `MERGE` to create nodes Cypher has a `CREATE` clause you can use for creating nodes. diff --git a/asciidoc/courses/cypher-fundamentals/modules/2-writing/lessons/3-create-relationships/lesson.adoc b/asciidoc/courses/cypher-fundamentals/modules/2-writing/lessons/3-create-relationships/lesson.adoc index 9aa1825c9..071e4fbad 100644 --- a/asciidoc/courses/cypher-fundamentals/modules/2-writing/lessons/3-create-relationships/lesson.adoc +++ b/asciidoc/courses/cypher-fundamentals/modules/2-writing/lessons/3-create-relationships/lesson.adoc @@ -1,14 +1,11 @@ = Creating Relationships :type: video :order: 3 +:slides: true [.video] video::9npLX5us1DU[youtube,width=560,height=315] -[NOTE] -.Video Transcript Correction -The actor's name for Michael Caine is misspelled in the video. It has been corrected in the transcript. - //https://youtu.be/9npLX5us1DU @@ -23,8 +20,13 @@ https://docs.google.com/document/d/1sZRzA7uqqkjcYee_vQM-KRBlILhkyZvEQa_PAX4CdHg/ [.transcript] +[.slide.discrete] == Creating a relationship between two nodes +[NOTE] +.Video Transcript Correction +The actor's name for Michael Caine is misspelled in the video. It has been corrected in the transcript. + In this lesson you will learn how to write Cypher clauses to create relationships between existing nodes in the graph. Just like you can use `MERGE` to create nodes in the graph, you use `MERGE` to create relationships between two nodes. @@ -34,7 +36,10 @@ When you create a relationship between two nodes, it must have: * Type * Direction -For example, if the _Person_ and _Movie_ nodes both already exist, we can find them using a `MATCH` clause before creating the relationship between them. +[.slide] +== Using MERGE to Create Relationships + +If the _Person_ and _Movie_ nodes both already exist, we can find them using a `MATCH` clause before creating the relationship between them. [source,cypher] ---- @@ -46,6 +51,9 @@ MERGE (p)-[:ACTED_IN]->(m) Here we find the two nodes that we want to create the relationship between. Then we use the reference to the found nodes to create the _ACTED_IN_ relationship. +[.slide] +== Verifying the Relationship + We can confirm that this relationship exists as follows: [source,cypher] @@ -58,6 +66,9 @@ By default, in Neo4j Browser, the visualization connects nodes that have relatio Notice also that you need not specify direction in the `MATCH` pattern since the query engine will look for all nodes that are connected, regardless of the direction of the relationship. +[.slide] +== Relationship Direction Matters + For example, if we specified this relationship pattern: [source,cypher] @@ -68,7 +79,8 @@ RETURN p, m This query returns no nodes since there are no nodes with the _ACTED_IN_ relationship to _Person_ nodes in the graph. -=== Creating nodes and relationships using multiple clauses +[.slide] +== Creating nodes and relationships using multiple clauses We can also chain multiple `MERGE` clauses together within a single Cypher code block. @@ -82,6 +94,9 @@ MERGE (p)-[:ACTED_IN]-(m) This code creates two nodes and a relationship between them. Because we have specified the variables _p_ and _m_, we can use them in the code to create the relationship between the two nodes. +[.slide] +== Relationship Direction Defaults + [TIP] .Relationship Direction Defaults ==== @@ -97,7 +112,8 @@ MATCH (p:Person {name: 'Chadwick Boseman'})-[:ACTED_IN]-(m:Movie {title: 'Black RETURN p, m ---- -=== Using `MERGE` to create nodes and a relationship in single clause +[.slide] +== Using `MERGE` to create nodes and a relationship in single clause What `MERGE` does is create the node or relationship if it does not exist in the graph. diff --git a/asciidoc/courses/cypher-fundamentals/modules/2-writing/lessons/5-updating-properties/lesson.adoc b/asciidoc/courses/cypher-fundamentals/modules/2-writing/lessons/5-updating-properties/lesson.adoc index 8043bb763..9ed7f4c53 100644 --- a/asciidoc/courses/cypher-fundamentals/modules/2-writing/lessons/5-updating-properties/lesson.adoc +++ b/asciidoc/courses/cypher-fundamentals/modules/2-writing/lessons/5-updating-properties/lesson.adoc @@ -1,13 +1,11 @@ = Updating Properties :type: video :order: 5 +:slides: true [.video] video::TUubik76iZ4[youtube,width=560,height=315] -[NOTE] -.Video Transcript Correction -The actor's name for Michael Caine is misspelled in the video. It has been corrected in the transcript. //https://youtu.be/TUubik76iZ4 @@ -23,14 +21,17 @@ https://docs.google.com/document/d/1TwXJ2vHQRlw-vQkg_DWduXNZDIkgxVJBSjHeYZcUerQ/ [.transcript] +[.slide.discrete] == Updating properties -Thus far, you have learned how to create nodes with `MERGE` where you specify the primary key property for the node. -You can add, modify, or remove properties from nodes and relationships. +[NOTE] +.Video Transcript Correction +The actor's name for Michael Caine is misspelled in the video. It has been corrected in the transcript.remove properties from nodes and relationships. In this lesson you will learn how to write Cypher code to update properties of nodes and relationships. -=== Adding properties for a node or relationship +[.slide] +== Adding properties for a node or relationship There are two ways that you can set a property for a node or relationship. @@ -48,11 +49,16 @@ MERGE (p)-[:ACTED_IN {roles: ['Alfred Penny']}]->(m) RETURN p,m ---- -In this code, the actor, _Michael Caine_ exists but the movie, _Batman Begins_ does not. We find the _Person_ node and we create the _Movie_ node. -Then, we create the _ACTED_IN_ relationship between the _Michael Caine_ node and the newly-created _Batman Begins_ node. And we set the _roles_ property for this relationship to an array of values - containing one value, _Alfred Penny_. -Notice that for inline property setting, we use the JSON-style of adding the property key/value pairs in braces `{ .. }`, just like we did when we specified the property for the node. +[.slide] +== Inline Property Setting Explained -==== 2. Using the `SET` keyword for a reference to a node or relationship +* In this code, the actor, _Michael Caine_ exists but the movie, _Batman Begins_ does not. +* We find the _Person_ node and we create the _Movie_ node. +* Then, we create the _ACTED_IN_ relationship between the _Michael Caine_ node and the newly-created _Batman Begins_ node. And we set the _roles_ property for this relationship to an array of values - containing one value, _Alfred Penny_. +* Notice that for inline property setting, we use the JSON-style of adding the property key/value pairs in braces `{ .. }`, just like we did when we specified the property for the node. + +[.slide] +== Using the `SET` keyword We also have the option to use the `SET` keyword for setting a property value. In the context of particular `MERGE` or `MATCH` clause where you have defined a variable to reference the node or relationship, you can set property values. @@ -65,7 +71,8 @@ SET r.roles = ['Alfred Penny'] RETURN p, r, m ---- -==== Setting multiple properties +[.slide] +== Setting multiple properties If you need to set multiple properties, you separate them with a comma (,). For example: @@ -77,7 +84,8 @@ SET r.roles = ['Alfred Penny'], m.released = 2008 RETURN p, r, m ---- -=== Updating properties +[.slide] +== Updating properties If you have a reference to a node or relationship, you can also use `SET` to modify the property. For example, if we wanted to modify _Michael Caine's_ role to be something different, we could do the following: @@ -90,7 +98,8 @@ SET r.roles = ['Mr. Alfred Penny'] RETURN p, r, m ---- -=== Removing properties +[.slide] +== Removing properties You can remove or delete a property from a node or relationship by using the `REMOVE` keyword, or setting the property to `null`. @@ -104,7 +113,10 @@ REMOVE r.roles RETURN p, r, m ---- -Here we remove the _born_ property from an actor: +[.slide] +== Alternative: Setting to null + +Here we remove the _born_ property from an actor by setting it to `null`: [source,cypher] ---- diff --git a/asciidoc/courses/cypher-fundamentals/modules/2-writing/lessons/7-merge-processing/lesson.adoc b/asciidoc/courses/cypher-fundamentals/modules/2-writing/lessons/7-merge-processing/lesson.adoc index cd397fed1..5e6478799 100644 --- a/asciidoc/courses/cypher-fundamentals/modules/2-writing/lessons/7-merge-processing/lesson.adoc +++ b/asciidoc/courses/cypher-fundamentals/modules/2-writing/lessons/7-merge-processing/lesson.adoc @@ -1,13 +1,12 @@ = Merge Processing :type: video :order: 7 +:slides: true [.video] video::3zHH1ei5eCw[youtube,width=560,height=315] -[NOTE] -.Video Transcript Correction -The actor's name for Michael Caine is misspelled in the video. It has been corrected in the transcript. + //https://youtu.be/3zHH1ei5eCw @@ -21,13 +20,20 @@ https://docs.google.com/document/d/1AFfffANGSB6-YpfjFJ6qRkxONnsw-PMrhFp1dz0qtLo/ //// [.transcript] +[.slide.discrete] == Merge processing + +[NOTE] +.Video Transcript Correction +The actor's name for Michael Caine is misspelled in the video. It has been corrected in the transcript. + You have learned that you can use `MERGE` to create nodes and relationships in the graph. `MERGE` operations work by first trying to find a pattern in the graph. If the pattern is found then the data already exists and is not created. If the pattern is not found, then the data can be created. -=== Customizing `MERGE` behavior +[.slide] +== Customizing `MERGE` behavior You can also specify behavior at runtime that enables you to set properties when the node is created or when the node is found. We can use the `ON CREATE SET` or `ON MATCH SET` conditions, or the `SET` keywords to set any additional properties. @@ -36,6 +42,9 @@ In this example, if the _Person_ node for _McKenna Grace_ does not exist, it is If the node is found, then the _updatedAt_ property is set. In both cases, the _born_ property is set. +[.slide] +== ON CREATE and ON MATCH Example + Run this Cypher code at least 2 times to observe what properties are set. You can see the properties in table view. [source,cypher] @@ -55,13 +64,20 @@ SET p.born = 2006 RETURN p ---- + +[.slide] +== Setting multiple properties + If you want to set multiple properties for an `ON CREATE SET` or `ON MATCH SET` clause, you separate them by commas. + For example: -`ON CREATE SET - m.released = 2020, m.tagline = `A great ride!'` -=== Merging with relationships +`ON CREATE SET + m.released = 2020, m.tagline = 'A great ride!'` + +[.slide] +== Merging with relationships You can use `MERGE` to create nodes or relationships: @@ -79,6 +95,9 @@ MERGE (m:Movie {title: 'The Cider House Rules'}) MERGE (p)-[:ACTED_IN]->(m) ---- +[.slide] +== Single Clause MERGE with Relationships + Another way your can create these nodes and relationship is as follows: [source,cypher] @@ -87,6 +106,9 @@ MERGE (p:Person {name: 'Michael Caine'})-[:ACTED_IN]->(m:Movie {title: 'The Cide RETURN p, m ---- +[.slide] +== Single Clause MERGE with Relationships Explained + Here is what happens in the query processor: 1. Neo4j will attempt to find a _Person_ node with the name _Michael Caine_. diff --git a/asciidoc/courses/cypher-fundamentals/modules/2-writing/lessons/9-delete/lesson.adoc b/asciidoc/courses/cypher-fundamentals/modules/2-writing/lessons/9-delete/lesson.adoc index ec0bd1149..8023f4dc0 100644 --- a/asciidoc/courses/cypher-fundamentals/modules/2-writing/lessons/9-delete/lesson.adoc +++ b/asciidoc/courses/cypher-fundamentals/modules/2-writing/lessons/9-delete/lesson.adoc @@ -1,6 +1,7 @@ = Deleting Data :type: video :order: 9 +:slides: true [.video] video::qNY4ltwe8po[youtube,width=560,height=315] @@ -13,18 +14,21 @@ https://docs.google.com/document/d/1IgJFtqQ0epVu3ywM7cNgbe6nuTxpz-TVTarGV74-pqs/ //// [.transcript] +[.slide.discrete] == Deleting data In a Neo4j database you can delete: -* nodes -* relationships -* properties -* labels +* Nodes +* Relationships +* Properties +* Labels To delete any data in the database, you must first retrieve it, then you can delete it. + You have already learned how to remove or delete properties from nodes or relationships. -=== Deleting a node +[.slide] +== Deleting a node Suppose you have created a _Person_ node for _Jane Doe_. @@ -43,7 +47,8 @@ MATCH (p:Person {name: 'Jane Doe'}) DELETE p ---- -=== Deleting a relationship +[.slide] +== Deleting a relationship Suppose we had our _Jane Doe_ node again where she was added as an actor in the movie, _The Matrix_. Run this code to create the node and the relationship. @@ -58,6 +63,9 @@ RETURN p, m This code creates one node and the relationship from _Jane Doe_ to _The Matrix_. +[.slide] +== Removing Only the Relationship + To leave the _Jane Doe_ node in the graph, but remove the relationship we retrieve the relationship and delete it. [source,cypher] @@ -67,6 +75,9 @@ DELETE r RETURN p, m ---- +[.slide] +== Cannot Delete Nodes with Relationships + Run this Cypher code that recreates the relationship: [source,cypher] @@ -77,6 +88,10 @@ MERGE (p)-[:ACTED_IN]->(m) RETURN p, m ---- + +[.slide] +== Try to Delete a Node with Relationships + If we attempt to delete the _Jane Doe_ node, we will receive an error because it has relationships in the graph. Try running this Cypher code: @@ -90,7 +105,8 @@ DELETE p You should receive an error. Neo4j prevents orphaned relationships in the graph. -=== Deleting a node and its relationships +[.slide] +== Deleting a node and its relationships Neo4j provides a feature where you cannot delete a node if it has incoming or outgoing relationships. This prevents the graph from having orphaned relationships. @@ -105,6 +121,9 @@ DETACH DELETE p This code deletes the relationship and the _Person_ node. +[.slide] +== Deleting all nodes and relationships + You can also delete all nodes and relationships in a database with this code. [NOTE] @@ -121,7 +140,8 @@ DETACH DELETE n .Use with Caution on Large Databases You should only do this on relatively small databases as trying to do this on a large database will exhaust memory. -=== Deleting labels +[.slide] +== Deleting labels A best practice is to have at least one label for a node. @@ -142,6 +162,9 @@ SET p:Developer RETURN p ---- +[.slide] +== Removing Labels + To remove the newly-added label, Developer, you use the `REMOVE` clause. Run this code: [source,cypher] @@ -152,9 +175,13 @@ RETURN p ---- The Jane Doe node has two labels, Person and Developer. You can use a `MATCH` to find that node. + Note that you could have specified `MATCH (p:Developer {name: 'Jane Doe'})` or `MATCH (p:Person:Developer {name: 'Jane Doe'})` to find the same node. Once we have a reference to that node, we can remove the label with the `REMOVE` clause. +[.slide] +== Cleanup and Listing Labels + And finally, delete the Jane Doe node by running this code: [source,cypher] @@ -165,14 +192,14 @@ DETACH DELETE p Note that `DELETE p` would also work in this case since we have not created any relationships. -[TIP] -.What labels exist in the graph? +[.slide] +== Retrieving Labels You can find out what labels exist in the graph with this code: [source,cypher] ---- -CALL db.labels() +CALL db.labels() ---- [.quiz] diff --git a/asciidoc/courses/cypher-fundamentals/modules/2-writing/module.adoc b/asciidoc/courses/cypher-fundamentals/modules/2-writing/module.adoc index 5206706c2..0e7b71145 100644 --- a/asciidoc/courses/cypher-fundamentals/modules/2-writing/module.adoc +++ b/asciidoc/courses/cypher-fundamentals/modules/2-writing/module.adoc @@ -1,9 +1,15 @@ = Writing Data to Neo4j :order: 2 +:slides: true +[.slide.discrete] +== Introduction In this module you will learn how to update the graph using Cypher and the Movies example dataset. +[.slide] +== What You Will Learn + You will learn to: * Use `MERGE` to create nodes in the graph. @@ -13,6 +19,7 @@ You will learn to: * Delete nodes and relationships from the graph. +[.slide] == Domain model for this course Again, here is the domain model and how it is represented in our graph: