【问题标题】:return seperate collection referening property of and return also nested collection from same query返回单独的集合引用属性,并返回来自同一查询的嵌套集合
【发布时间】:2014-12-09 15:25:07
【问题描述】:

我正在从密码查询构建一个嵌套对象(一个嵌套树,一对多......或者每个节点在每个深度级别都有一对多),我将返回一个包含嵌套的值对象,另一个包含仅包含最后一个嵌套对象对象中的 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

但是,它似乎将结果分解为多个重复值,而如果我删除初始集合,则树会正确构建。例如

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 的单独结果列,我就是这样做的不希望遍历密码结果来收集它们。

【问题讨论】:

  • 哪些对象是一对多连接的?以及您尝试为哪种对象类型收集:ObjectThings。从第一个查询的输出中,我猜测 :ObjectC:ObjectThing 是一对一相关的,这就是它返回单项文档数组的原因
  • 是的,对不起,从左到右,它是 1 到多,所以 a(1 到多)到 b,b(1 到多)b...我会更新这个问题,很好,谢谢。
  • 让我知道我的编辑是否传达了 (a) 1 到多个 (b)、每个 (b) 一个到多个 (c)、每个 (c) 一个到多个 (东西)。
  • 它仍然让我感到困惑,你有 c.reference = thing.uuid,这表明它是从 :ObjectC 到 :ObjectThing 的 *-to-1 关系(否则它应该是 c.references = collect (东西.uuid))。如果是这种情况,请在下面查看我的答案

标签: neo4j cypher


【解决方案1】:

我认为你也必须聚合 uuid:

即类似:

  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

如果你不想拥有嵌套的 uuid 集合,你可以这样做

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

【讨论】:

    【解决方案2】:

    您需要在同一个WITH 子句中收集thing id 和ObjectCs:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-25
      相关资源
      最近更新 更多