Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
07-15-2019 02:13 AM
I have a "model" with players, teams and stadiums
Players below=> teams
players play in =>stadiums
I have some players that do not below to any teams.
Example:
John is a player of the TEAM A, and play in "central stadium"
Mike is a player that play in "central stadium"
Mike has NO relation with any team.
Can I perform a query that when I ask for the Mike TEAM is show to me that Mike is related with "TEAM A" ? Just try to infere the TEAM
07-15-2019 02:22 AM
I'm not entirely sure what you're asking for here.
If you want to get Mike's team, but not wipe out the row if he doesn't belong to a team, use an OPTIONAL MATCH to the team, and you can use coalesce() to provide a default if a null is encountered:
MATCH (m:Player {name:'Mike'})
OPTIONAL MATCH (m)-[:BELONGS_TO]->(t:Team)
RETURN coalesce(t.name, 'NOT ASSIGNED') as teamName
07-15-2019 06:17 AM
I want to try to do something like the following
John plays for "TEAM A" in the "central stadium" so if Mike plays also in the central stadium Mike should play in the "TEAM A"
07-15-2019 07:41 AM
Okay, do you want a query that will set this data for you based on that, or do you want it to not write to the db but return that info at query time?
07-15-2019 08:35 AM
Not to write. Only in query time
07-15-2019 08:43 AM
Okay, something like this maybe? Note that if there are people from multiple teams that play in the same stadium then this will pick one.
MATCH (p:Player {name:'Mike'})
OPTIONAL MATCH (p)-[:BELONGS_TO]->(team)
WITH p, team
MATCH (p)-[:PLAYS_IN*2]-()-[:BELONGS_TO]->(someTeam)
WITH team, collect(DISTINCT someTeam)[0] as fallbackTeam
RETURN coalesce(team.name, fallbackTeam.name, 'UNASSIGNED') as assignedTeam
All the sessions of the conference are now available online