Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

How to continue executing a Neo4J Cypher request after MATCH is not matched?

I have the following params in my Neo4J:

{
  "lists": [
    {
      "from": "someone",
      "to": "somebody"
    }
  ]
}

And the following query:

MATCH (c:Concept{name:'infranodus'}) WITH c, $lists AS list
UNWIND CASE WHEN list = [{}] THEN [null] ELSE list END AS l 
WITH l 
MATCH (cp1:Concept{name:l.from}) 
WITH cp1 
MATCH (cp2:Concept{name:'somebody'}) 
RETURN cp1,cp2;

The query above will work.

However, if I replace l.from with a non-existent parameter, e.g. l.about, then — as the match doesn't happen —?the second cp2 match doesn't fire.

How can I change this behavior and continue executing this query even if cp1 is not found? Maybe there's a way to pass on a dummy variable as a result?

MATCH (c:Concept{name:'infranodus'}) WITH c, $lists AS list
UNWIND CASE WHEN list = [{}] THEN [null] ELSE list END AS l 
WITH l
MATCH (cp1:Concept{name:l.about}) 
WITH cp1 
MATCH (cp2:Concept{name:'somebody'}) 
RETURN cp1,cp2;
question from:https://stackoverflow.com/questions/65600229/how-to-continue-executing-a-neo4j-cypher-request-after-match-is-not-matched

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use OPTIONAL MATCH. If there is no match is found, then it will use NULL for the missing part of the pattern. It is similar to outer join in SQL.

NEW:
OPTIONAL MATCH (cp1:Concept{name:l.about}) 

OLD:
MATCH (cp1:Concept{name:l.about}) 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...