How to collect node property and relationship property together

cypher

(Henry Macafee) #1

Hi there! I'm trying to return what I can only best describe as a subquery. For example:

(a:User)-[:CREATED]->(b:Item)
(c:User)-[r:PURCHASED]->(b)

What I'd like to return is something like:
a.name, b.name, {c.name: r.date}
as there are multiple c:nodes and r:relationships per b:node.

I've looked into map projections but keep hitting a dead end. Any suggestions?

Thank you for your time


(Andrew Bowman) #2

The tricky part of this is that with Cypher alone you cannot create a map with a dynamic property key (c.name), it has to be literal.

There is a workaround you can use with helper functions from APOC Procedures, but make sure you actually need this as it complicates the query.

MATCH (a:User)-[:CREATED]->(b:Item) 
WITH a.name as creator, b.name as item, b
MATCH (c:User)-[r:PURCHASED]->(b)
RETURN creator, item, apoc.map.fromLists([c.name], [r.date]) as purchaser

(Henry Macafee) #3

Awesome, I think this did the trick. Thank you!