返回单独的集合引用属性,并从同一查询返回嵌套集合

时间:2014-12-09 15:25:08

标签: neo4j cypher

我在cypher查询中构建一个嵌套对象(一对多的嵌套树......或者每个节点在每个深度级别都有一对多),我将返回一个包含的值嵌套对象,另一个包含最后一个嵌套对象对象中只有ID的数组。这是两个结果列,其中一个包含整个树,另一个只包含ID。

使用:Seraph queryRaw(),Neo4j 2.1.3

MATCH (oa:ObjectA)-[:has]->(ob:ObjectB)-[:has]->(oc:ObjectC)<-[:extends]-(thing:ObjectThing)
      WHERE ob.year = 2012
      WITH oa, ob, oc, collect(thing.uuid) as uuids
      WITH uuids, oa, ob, collect({name:oc.name, uuid:oc.uuid, reference:thing.uuid}) AS objectCs
      WITH uuids, oa, collect({name:ob.name, year:ab.year, objectCs:objectCs}) AS objectBs
      RETURN {name:oa.name, country:oa.country, objectCs:objectCs} AS objectAs, uuids as documents

然而,它似乎将结果分解为多个重复值,而如果我删除了初始收集,则树正确构建。 e.g。

columns: [ 'objectAs', 'documents']
data: 
[
  [ 
    { 
      name:'oa name', 
      country: 'oa country',
      objectBs: 
        [    // one to many
          {
            name: 'ob name',
            year: 2013...
             ... array of one to many oc nested objects ...}
          }
        ]
    },
    [ '0000494949499' ]
  ],
  [
    { 
      name:'oa other name', 
      country: 'oa other country',
      objectBs: [{ ... one to many nested objects like above...}]
    },
    [ '1234124331243' ]
  ]
]

MATCH (oa:ObjectA)-[:has]->(ob:ObjectB)-[:has]->(oc:ObjectC)<-[:extends]-(thing:ObjectThing)
      WHERE ob.year = 2012
      WITH oa, ob, collect({name:oc.name, uuid:oc.uuid, reference:thing.uuid}) AS objectCs
      WITH oa, collect({name:ob.name, year:ab.year, objectCs:objectCs}) AS objectBs
      RETURN {name:oa.name, country:oa.country, objectCs:objectCs} AS objectAs

columns: [ 'objectAs']
data: 
[
  [ 
    { 
      name:'oa name', 
      country: 'oa country',
      objectBs: [{ ... nested objects ...}]
    },
    { 
      name:'oa other name', 
      country: 'oa other country',
      objectBs: [{ ... nested objects ...}]
    }
  ]
]

我想获得以下内容:

columns: [ 'objectAs', 'documents']
data: 
[
  [     // index 0 would represent column 'objectAs'
    { 
      name:'oa name', 
      country: 'oa country',
      objectBs: [{ ... nested objects ...}]
    },
    { 
      name:'oa other name', 
      country: 'oa other country',
      objectBs: [{ ... nested objects ...}]
    }
  ]
  [    // index 1 would represent column 'documents'
    '0000494949499', 
    '1234124331243' 
  ]

]

简而言之:我只是想在4个级别中构建所有一对多的嵌套结果但是在最后一个(事物)节点中找到的所有UUID都有一个单独的结果列,也就是我做的不希望迭代cypher结果来收集它们。

2 个答案:

答案 0 :(得分:0)

我认为你也必须聚合uuids:

即。类似的东西:

  MATCH (oa:ObjectA)-[:has]->(ob:ObjectB)-[:has]->(oc:ObjectC)<-[:extends]-(thing:ObjectThing)
  WHERE ob.year = 2012
  WITH oa, ob, oc, collect(thing.uuid) as uuids
  WITH collect(uuids) as uuids, oa, ob, collect({name:oc.name, uuid:oc.uuid, reference:thing.uuid}) AS objectCs
  WITH collect(uuids) as uuids, oa, collect({name:ob.name, year:ab.year, objectCs:objectCs}) AS objectBs
  RETURN {name:oa.name, country:oa.country, objectCs:objectCs} AS objectAs, uuids as documents

如果你不想拥有你可以做的嵌套的uuids集合

  reduce(a=[]; x in collect(uuids) | a + x)

答案 1 :(得分:0)

您需要在同一thing条款中收集ObjectC ID和WITH

MATCH (oa:ObjectA)-[:has]->(ob:ObjectB {year: 2012})-[:has]->(oc:ObjectC)<-[:extends]-(thing:ObjectThing)
    WITH oa, ob, collect({name:oc.name, uuid:oc.uuid, reference:thing.uuid}) AS objectCs, collect(thing.uuid) as uuids
    WITH uuids, oa, collect({name:ob.name, year:ab.year, objectCs:objectCs}) AS objectBs
    RETURN {name:oa.name, country:oa.country, objectCs:objectCs} AS objectAs, uuids as documents