cancel
Showing results for 
Search instead for 
Did you mean: 

Cypher Query Help

akshat_mittal
Graph Buddy

Hi Team,

 

I need a help regarding the below query - In this query i want to set a property on a node using SET in combination with  WITH clause.

MATCH (h:EMDB1) WHERE h.DLM IS NOT NULL
WITH h.NEW_DLM_DATA_2 AS H2, h.NEW_DLM_DATA_1 AS H1 , count(h.NEW_DLM_DATA_2) as movies
WHERE movies > 1 
SET h.MFD='Y'
RETURN H2, movies ,H1 
 
ERROR - 
Variable `h` not defined (line 4, column 5 (offset: 153))
"SET h.MFD='Y'"

Regards

Akshat

2 REPLIES 2

ameyasoft
Graph Maven

Try this:

MATCH (h:EMDB1) WHERE h.DLM IS NOT NULL
WITH h,  h.NEW_DLM_DATA_2 AS H2, h.NEW_DLM_DATA_1 AS H1 , count(h.NEW_DLM_DATA_2) as movies
WHERE movies > 1 
SET h.MFD='Y'
RETURN H2, movies ,H1 

glilienfield
Ninja
Ninja

Your query does not result in a list of nodes that you can then set their ‘MFD’ property. 
What your query does is find all the ’EMDB1’ nodes whose ‘DLM’ is not null. For each node, you extract with the 'with' clause just the ‘NEW_DLM_DATA_1’ and ‘NEW_DLM_DATA_2’ properties, as 'H1' and 'H2' respectively. You perform an aggregation in the with clause, so the result is a list of rows consisting of each unique combination of the ‘NEW_DLM_DATA_1’ and ‘NEW_DLM_DATA_2’ properties with a count of how many times that combination existed in the original set of rows from the match clause.  As such, all you have after the ‘with’ clause are rows consisting of three items, two property values and a number. The nodes represented by ‘h’ are no longer in scope, thus you can not set their ‘MFD’ property. 

If your intent is to set only those nodes whose combination of ‘NEW_DLM_DATA_1’ and ‘NEW_DLM_DATA_2’ properties exists more than once in the set of 'EMDB1' nodes, you will need to match again to get those nodes to set the ‘MDF’ property. You would do so by inserting the following  ‘match’ clause between your ‘with’ and ‘set’ clauses, which would give you the nodes to set.

 

 

match(h:EMBD1)
where h.NEW_DLM_DATA_1 = H1 and NEW_DLM_DATA_2 = H2

 

 

The issue with this is that you would no longer have the values of 'H1', 'H2', and 'movies' (their count) to return. To get around this, the solution is to perform the second 'match' and 'set' operations in a 'call' subquery as follows:

 

 

MATCH (h:EMDB1) 
WHERE h.DLM IS NOT NULL
WITH h.NEW_DLM_DATA_2 AS H2, h.NEW_DLM_DATA_1 AS H1, count(*) as movies
WHERE movies > 1 
CALL {
    WITH H1, H2
    MATCH (h:EMDB1) 
    WHERE h.NEW_DLM_DATA_1 = H1
    AND h.NEW_DLM_DATA_2 = H2
    SET h.MFD='Y'
}
RETURN H1, H2, movies

 

If you don't really need to return the values of H1, H2, and movies, you can simplify the query as follows:

MATCH (h:EMDB1) 
WHERE h.DLM IS NOT NULL
WITH h.NEW_DLM_DATA_2 AS H2, h.NEW_DLM_DATA_1 AS H1 , count(h.NEW_DLM_DATA_2) as movies
WHERE movies > 1 
MATCH (h:EMDB1) 
WHERE h.NEW_DLM_DATA_1 = H1
AND h.NEW_DLM_DATA_2 = H2
SET h.MFD='Y'
Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.