因为Neo4j为每个返回对象返回一行,所以它可以返回具有重复属性的行。
如何将这组行转换为合适的数据结构?具体来说,我想梳理每一行并将重复值设置为属性,对于我希望不同的那些,将它们添加到数组中,以便最终得到更具可读性的内容。例如,转过这样的东西:
[
[“event” = “My Event”, “creator” = “Adam”, “occurrence” = “1/2/3”],
[“event” = “My Event”, “creator” = “Adam”, “occurrence” = “2/3/4”],
[“event” = “My Event”, “creator” = “Adam”, “occurrence” = “3/4/5”],
[“event” = “Event 2”, “creator” = “Someone”, “occurrence” = “4/2/3”],
[“event” = “Event 2”, “creator” = “ Someone”, “occurrence” = “4/3/4”],
[“event” = “Event 2”, “creator” = “ Someone”, “occurrence” = “6/4/5”],
]
以更可读和更混乱的方式:
[
[“event” = “My Event”, “creator” = “Adam”, “occurrence” = [“1/2/3”, “2/3/4 ”, “3/4/5”]],
[“event” = “Event 2”, “creator” = “ Someone”, “occurrence” = [“4/2/3”, “4/3/4”, “6/4/5”]]
]
更新:
这是我的查询,
MATCH (creator:CreatingUser)-[:creator]-(event:Event), (occurrence:EventSpaceTimeOccurrence)-[:occurrence]-(event)
WHERE creator.id = '1234'
RETURN event, creator, occurrence
我期待一个事件,一个eventType,一个创建者和多次出现。上面的查询给出了上面的结果,我想将其折叠到上面的非杂乱版本中。
答案 0 :(得分:1)
在您编辑后,我会建议以下
MATCH (creator:CreatingUser)-[:creator]-(event:Event), (occurrence:EventSpaceTimeOccurrence)-[:occurrence]-(event)
RETURN event, creator, collect(occurrence) as eventoccurrences
这将导致每个事件和创建者的唯一组合一行,以及此组合的一系列事件。如果您只对特定属性感兴趣,那么您可以收集这些属性。例如,如果您有eventdate
属性,则可以使用
RETURN event, creator, collect(occurrence.eventdate) as eventdates
现在,您将获得eventdate
财产的集合。不幸的是,我不熟悉PHP语言驱动程序,所以不能建议一个函数。我知道在其他语言中有相应的收集。 HTH!