我有一个图表模型,其中有post
个节点代表帖子,comment
个节点代表帖子的评论:
(p:post)<-[:ON]-(c:comment{role:'question'})
(p:post)<-[:ON]-(c:comment{role:'criticism'})
每个评论都有一个role
属性,它是五个可能值之一。我想编写一个查询,返回给定计划中每个角色的注释总数。
这:
MATCH (:item:initiative{urlCode:'AAECyS'})<-[:TO]-(c:comment) WITH
COLLECT(c) AS comments
RETURN {
question:length(filter(x IN comments WHERE x.role = 'question')),
criticism:length(filter(x IN comments WHERE x.role = 'criticism'))
//etc. for other three types
}
给了我我想要的东西 - 但对我来说感觉非常难看。
有更干净/更有效的方法吗?
答案 0 :(得分:0)
你可以把它们放在一个集合中,然后返回一个相应的集合,这样会更整洁。
MATCH (:item:initiative{urlCode:'AAECyS'})<-[:TO]-(c:comment)
WITH [c.role, count(*)] as role_count
RETURN collect(role_count) as role_counts
答案 1 :(得分:0)
可能是最简单的查询(每个角色都有一行):
MATCH (:item:initiative{urlCode:'AAECyS'})<-[:TO]-(c:comment)
RETURN c.role, count(*);