我使用以下cypher获取标签User的所有实体:
MATCH (u:User)
OPTIONAL MATCH (u)-[r:RATED]->(m:Movie)
WITH u,r,m
RETURN u.id as uid, m.id as mid, r.rating as rating
这会返回一个格式为:
的对象数组[
{
"uid": "user1",
"mid": "bettercallsaul",
"rating": 9
},
{
"uid": "user1",
"mid": "breakingbad",
"rating": 10
}
// ... etc
]
我希望按u.id
(或uid
)对其进行分组,以便返回类似(或类似)的内容:
[
{"user1":
{"mid": "bettercallsaul", "rating": 9},
{"mid": "breakingbad", "rating": 10},
// ... etc
},
{"user2":
{"mid": "breakingbad", "rating": 7},
{"mid": "bettercallsaul", "rating": 8},
// ... etc
}
// ... etc
]
答案 0 :(得分:1)
我认为你正在寻找COLLECT()函数。
也许尝试这个查询:
MATCH (u:User)
OPTIONAL MATCH (u)-[r:RATED]->(m:Movie)
RETURN u.id,COLLECT({mid:m.id, rating:r.rating}) as movieRatings
如果您使用的是Neo4j 3.1.x或更高版本,则可以使用模式理解和地图投影来简化:
MATCH (u:User)
RETURN u.id as user,
[(u)-[r:RATED]->(m:Movie) | m {.id, rating:r.rating}] as movieRatings
如果您希望movieRatings列表成为每个用户属性映射的一部分,您也可以使用map projection来执行此操作:
MATCH (u:User)
RETURN u {.id, .name, movieRatings:
[(u)-[r:RATED]->(m:Movie) | m {.id, rating:r.rating}] } as user
答案 1 :(得分:1)
我不知道是否完全可以做你想做的事。 但是我想你会接近Neo的collect function。
MATCH (u:User)
OPTIONAL MATCH (u)-[r:RATED]->(m:Movie)
WITH u.id as uid, m.id as mid, r.rating as rating
RETURN uid, collect([mid, rating])