1
votes

I am trying to find the most optimal way of returning all matched nodes and any relationships they might have?

Here's my problem:

I need to return all users who created a project, so

match (u : User)-[r:CREATE]->(p: Project) return u, collect(p)

Simple enough, but User could also have other relationships and I would like to include them or optionally check (return true/false)

For example User could have relationship RECOMMEND, I don't want to limit by it, but if check if it exists an with what node?

Ideally my return table would look like this:

USER1 - PROJECT(S) - RECOMMENDED USER

USER2 - PROJECT(S) - NULL (nobody is recommending)

1

1 Answers

2
votes

OPTIONAL MATCH will match the pattern and return null if it does not exist

MATCH (u : User)-[r:CREATE]->(p: Project) 
OPTIONAL MATCH (u)-[:RECOMMEND]->(rec)
RETURN u, collect(p), collect(rec)