Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
01-28-2022 04:19 PM
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?
Solved! Go to Solution.
01-28-2022 04:28 PM
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}]}
01-28-2022 04:28 PM
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}]}
01-28-2022 04:49 PM
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
01-28-2022 05:01 PM
Ah, there you go, I overlooked that you were missing key:value syntax. That's needed when working with map literals.
01-28-2022 04:35 PM
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}
01-28-2022 04:50 PM
Are there any performance implications to using this method of a second match and collect vs the subquery and projection?
01-28-2022 04:52 PM
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}
01-28-2022 04:54 PM
Are you saying that the performance is equivalent?
01-28-2022 05:00 PM
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.
01-28-2022 05:03 PM
Well thank you to you both. Both are good solutions!
All the sessions of the conference are now available online