Neo4j / Cypher: If not exists, get that instead

I’m starting to learn Cypher recetly since I’m doing a project backed with a Neo4j database.

One thing I bumped in to recently was a situation like this:

> A user can post a link. Users can comment on that link, and on other comments as well. (think reddit).
If someone comments on your link or on one of your comments, you should get an email saying so.

So, we have: (n:user)<-[:POSTED_BY]-(m:link) for the link.

And for the comments we have (n:user)-[:POSTED]->(o:comment)-[:COMMENT]-(m:link)

When you post a link or add a comment, a SUBSCRIBES_TO relation is added. Like this:


Now, when a user posts a comment I want to alert everybody (there will be an “Subscribe to answers” button on all comment so it’s not just the comment/link submitter that are subscribing) that subscribes to the comment or link.
And, I want to include the link.title in that notifiction email.

Like “A new comment was posted on XXX“ where XXX is the link title.

This is where is gets tricky. I want an answer in this format:
{user_email:, link_title: m.title}

So, how do I do this?
The only difference between comments and links I may use is that comments has a parent property.
All I have is an ID that could be the ID of a link or of a comment.

This is what I’ve come up with. It works but if I wanted more properties from the link there will be an ugly query.

MATCH (n:user)-[r:SUBSCRIBES_TO]->(m)
WHERE ID(m)={id}
WHERE NOT HAS(o.parent)
WITH n,m,o
RETURN {news_title: CASE WHEN m.parent IS NULL THEN m.title ELSE o.title END, email:}

Is there a better way?

Optimally there would be some kind of IF NOT m THEN instead of the OPTIONAL MATCH so I could overwrite m with a new node so there’s no need for the CASE clause.

Update 1
Neo4j got back to me on Twitter (thanks!)

@oskarhane try coalesce(o.title, m.title) will return the first non null argument

— Neo4j (@neo4j) February 5, 2014