Anurag Sharma
Anurag Sharma

Reputation: 5039

Get all connecting node in neo4j for all nodes matching a query

I have 2 types of node in my neo4j db

Skill and SkillCluster

A Skill node as a directed edge[BelongsTo relationship] to one or more SkillCluster nodes. I want to find all the skills name and its connecting skillcluster names that belongs to a specific skill-cluster. I have written a query to find all skills belonging to a skill-cluster like this -

match(cluster:SkillCluster {Name: 'Engineering'})<-[:BelongsTo]-(skill:Skill) return skill.Name, cluster.Name;

This query is returning this output

skill.Name                                      cluster.Name
"Dxdesigner"                                    "Engineering"
"Electromagnetic Interference (EMI) Engineer"   "Engineering"
"SEAM 3D"                                       "Engineering"
"Electromagnetic Interference (EMI) Mitigation" "Engineering"

skill Dxdesigner connects to 2 skill-cluster nodes, but my query is not returning both of them.

Upvotes: 0

Views: 213

Answers (1)

Vivek Kumar
Vivek Kumar

Reputation: 36599

Try:

match(cluster:SkillCluster {Name: 'Engineering'})<-[:BelongsTo]-
                                       (skill:Skill)-
                                       [:BelongsTo]->(cluster2:SkillCluster) 
return skill.Name, cluster.Name, cluster2.Name;

Also, you dont need to return the cluster.Name as you have already set the Name to "Engineering". So it will always return engineering.

Update: As per InverseFalcon's comment:

match(cluster:SkillCluster {Name: 'Engineering'})<-[:BelongsTo]-
                                       (skill:Skill)-
                                       [:BelongsTo]->(cluster2:SkillCluster) 
return skill.Name, collect(cluster2.Name) + cluster.Name as clusters;

Upvotes: 1

Related Questions