Syntax for Multiple Variables in Or Query

I am learning the Cypher querying language using the sample movie database provided by neo4j and I had a question about syntax if I want to use multiple variables in an OR query.

If I wanted to run a query similar to that below, to assign the variable m to movie nodes and p to person nodes, how should I write the following query? I tried a number of different variations with &, {}, and multiple () but nothing is correct, and the document is silent on how (or even whether it is possible) to write such a query.

<MATCH (m:Movie|p:Person)
RETURN p.name AS name,m.title AS title/>

Thank you.

Your syntax is not allowed, as you are trying to assign a node to two variables.

You could do this in different ways, but I would need more info on why you are querying for two unrelated entities in the same query.

Here are some legal solutions to give you an idea.

MATCH (n:Movie|Person)
RETURN n.name AS name, n.title AS title

Note, one of the properties in each row will be null, since movie and person nodes don’t share those two properties.

MATCH (m:Movie)
RETURN “Movie” as type, m.title AS name
UNION
MATCH (p:Person)
RETURN “Person” as type, p.name AS name

Note, union requires the column names be the same, so I chose “name” as the common label.

Thank you for the reply. I was just interested if different variables could be assigned to different nodes in an OR, or AND match. I do not have a particular use case.

When you are using an OR or AND with labels in a match statement, you are still searching for a single entity. That is why you bind the results to one variable.

The results of each match statement can be bound to different variables.