Week 11 Sample Solution

Sample solution for Week 11 lab

C.3. List and Pattern Comprehension

Combine the two above into one block of queries to reduce redundant code.

MATCH (jobi:Cook {name: 'Jobi'})
WITH jobi, [(jobi)-->(b:Person) | b.occupation] AS jobi_circle_occupation
UNWIND jobi_circle_occupation AS occupation
WITH jobi, collect(DISTINCT occupation) AS distinct_occupations
SET jobi.circle_occupations = distinct_occupations
RETURN jobi

Modify the third code chunk and try to list the people with the number of occupation types related to them in desending order. e.g. (if the circle_occupation of Alice is ["Mechanic", "Actor"], the result should be Alice 2)

MATCH (n:Person)
WITH n, [(n)-->(b:Person) | b.occupation] AS my_circle_occupations
UNWIND my_circle_occupations AS occupations
WITH collect(DISTINCT occupations) AS distinct_occupations, n
SET n.num_circle_occupations = SIZE(distinct_occupations)
RETURN n.name,n.num_circle_occupations
ORDER BY n.num_circle_occupations DESC

C.4. Virtual Relationships and Virtual Nodes (Graph Projections)

Adapt the above code to create such virtual nodes for all cities.

MATCH (p:Person)-[:LIVES_IN]->(c:City)
WITH c, collect(p.name) AS residents, count(p) AS count
CALL apoc.create.vNode(residents,{name: residents}) YIELD node AS a
CALL apoc.create.vRelationship(a, "LIVES_IN", {count: count}, c) YIELD rel
RETURN a, c, rel

Last updated