cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Get all nodes linked by a specific relationship

I have created a vehicle make called `Toyota` and also created a new node store my vehicle models.

CREATE (Make:make{name: "Toyota"})
CREATE (Model:model {name: "Camry"})
CREATE (Make)-[r:MODEL_OF]->(Model)
RETURN Make, Model

I added two more vehicle models

CREATE (Model:model {name: "Vitz"})
CREATE (Model:model {name: "Corolla"})

and linked the two models to `Toyota`

MATCH (a:make), (b:model) WHERE a.name = "Toyota" AND b.name = "Vitz"
CREATE (a)-[r: MODEL_OF]->(b)
RETURN a,b


MATCH (a:make), (b:model) WHERE a.name = "Toyota" AND b.name = "Corolla"
CREATE (a)-[r: MODEL_OF]->(b)
RETURN a,b

How can i return all models of make `Toyota` that have the relationship `MODEL_OF`?

1 ACCEPTED SOLUTION

Try this:

match(n:make{name:"Toyota"})-[:MODEL_OF]->(m:model)
return m.name as model

Just a note, I would have the relationship in your model directed the opposite way, i.e. a model is-a model of a make.

Screen Shot 2022-10-08 at 11.42.18 AM.png 

View solution in original post

3 REPLIES 3

Try this:

match(n:make{name:"Toyota"})-[:MODEL_OF]->(m:model)
return m.name as model

Just a note, I would have the relationship in your model directed the opposite way, i.e. a model is-a model of a make.

Screen Shot 2022-10-08 at 11.42.18 AM.png 

I have reversed the relationships

reversedScreenshot_6.png

CREATE (Make:make{name: "Toyota"})
CREATE (Model:model {name: "Camry"})
CREATE (Model:model {name: "Vitz"})
CREATE (Model:model {name: "Corolla"})

Relationships

MATCH (a:model), (b:make) WHERE a.name = "Vitz" AND b.name = "Toyota"
CREATE (a)-[r: MODEL_OF]->(b)
RETURN a,b

MATCH (a:model), (b:make) WHERE a.name = "Corolla" AND b.name = "Toyota"
CREATE (a)-[r: MODEL_OF]->(b)
RETURN a,b

MATCH (a:model), (b:make) WHERE a.name = "Camry" AND b.name = "Toyota"
CREATE (a)-[r: MODEL_OF]->(b)
RETURN a,b

How can i modify your query?

Solved it like this

MATCH (Make:make {name: "Toyota"})<-[r:MODEL_OF]-(n)
RETURN n.name as Model