Limit match results per row in path

Hello Community,
I'm using Neo4j 4.1 and neo4j-driver for nodeJS.

My data looks like this:

image
where INVITED_BY has a property 'wid' that indicates to what workspace a user invited another.

I'm using a frontend library to show nodes, and I'm working with all Cypher queries that returns paths, like:

match path=(n:Workspace)-[]-()
  where n.wid = '${nodeId}'
  return path
  limit 50`

This time I need to show top 'inviters', and show people invited by them, but limited to a few nodes.
I found this very useful link:

But I'm unable to adapt it to my model AND STILL return a path, instead of just the nodes.
If I run this query on Neo4j browser, it looks great:

match (promoter:Member)<-[r:INVITED_BY]-(invited:Member)
match (invited)-[r]->(promoter)
return promoter, collect(invited)[..5]

but the actual data returned (under table or text tab, or even just running the query with NodeJS driver) doesn't have the relationships.

I tried this, but it after I collect the nodes, I have a list, and isn't assignable to a node. And I'm stuck :sob:

match (promoter:Member)<-[r:INVITED_BY]-(invited:Member)
match path=(invited)-[r]->(promoter)
with collect(invited)[..5] as invi
match path2=(invi)-[r]->(promoter)
return path2

I just need to limit the invited nodes but still return a path.
Any help will be appreciated.

WIth Neo4j 4.1.x you have some good options here, we can use subqueries to LIMIT the paths to return. Something like this:

MATCH (promoter:Member)
WHERE (promoter)<-[:INVITED_BY]-()
CALL {
  WITH promoter
  MATCH path = (promoter)<-[r:INVITED_BY]-(invited:Member)
  WITH path
  LIMIT 5
  RETURN collect(path) as invitePaths
}
RETURN promoter, invitePaths

You can play with the path elements or what to return from the subquery, the point is that with the subquery you can use LIMIT and it will apply only to the matches from each promoter rather than limiting all result rows.

1 Like

That's absolutely right. Thank you @andrew_bowman !