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.

Using list comprehension for get both node and edge property together

I'm trying to figure out how to run a subquery and combine an edge property with the node property, but I can't figure out how. What I would like to do is something like:

MATCH (person:Person)
RETURN person {.id, .name, Homes: [(person)-[r:OWNS]-(p:Property) | <not sure what to do here>{p.address, r.purchaseDate}]}

I have tried any combination of p and r for the list comprehension and nothing seems to work. Is this possible, or do I need to return these values separately?

1 ACCEPTED SOLUTION

That's interesting, the literal map should have been valid. Which version of Neo4j were you using?

As a workaround, you can use map projection again to get around that syntax error:

MATCH (person:Person)
RETURN person {.id, .name, Homes: [(person)-[r:OWNS]-(p:Property) | p {.address, purchaseDate:r.purchaseDate}]}

View solution in original post

9 REPLIES 9

That's interesting, the literal map should have been valid. Which version of Neo4j were you using?

As a workaround, you can use map projection again to get around that syntax error:

MATCH (person:Person)
RETURN person {.id, .name, Homes: [(person)-[r:OWNS]-(p:Property) | p {.address, purchaseDate:r.purchaseDate}]}

Ah thank you! It also works if I just use

{address:p.address, purchaseDate:r.purchaseDate}

So it's something to do with needing to name the relationship property

Ah, there you go, I overlooked that you were missing key:value syntax. That's needed when working with map literals.

Try this:

match(p:Person{id:100})
match(p)-[r:OWNS]->(h:Home)
with p, collect({address: h.address, purchaseDate: r.purchaseDate}) as homes
return p{.id, .name, homes: homes}

Are there any performance implications to using this method of a second match and collect vs the subquery and projection?

I just like the readability, but you can merge the two matches:

match(p:Person{id:100})-[r:OWNS]->(h:Home)
with p, collect(h{.address, purchaseDate: r.purchaseDate}) as homes
return p{.id, .name, homes: homes}

Are you saying that the performance is equivalent?

That I do not know. Your query is performing two queries, one in the initial match for the person, and then a second in the pattern match for the home when creating your collection of homes.

Well thank you to you both. Both are good solutions!