英文:
Neo4j apoc.convert.toTree is not showing duplicate children with unique relationship
问题
I have two nodes A and B (A->B) connected via two relationships. Those 2 relationships are unique. For example, you can think of the two rels as two paths from A to B at two different times. Converting them to one relationship is not viable at this time.
For displaying in UI, query result should be a tree structure. Used following query to get the tree
the resulting tree is
Preferred result is
How can I get a tree with duplicate children is more than one relationship exists between same two nodes?
Thanks.
英文:
I have two nodes A and B (A->B) connected via two relationships. Those 2 relationships are unique. For example, you can think of the two rels as two paths from A to B at two different times. Converting them to one relationship is not viable at this time.

For displaying in UI, query result should be a tree structure. Used following query to get the tree
MATCH p=(n:Label1 {name:'main'})-[:calls*..2]->(m)
WITH COLLECT(p) AS ps 
CALL apoc.convert.toTree(ps) yield value
RETURN value;
the resulting tree is
Preferred result is
How can I get a tree with duplicate children is more than one relationship exists between same two nodes?
Thanks.
答案1
得分: 0
我在这个SO问题上提供了一个解决方法。虽然它不完全等同于apoc函数生成的嵌套文档,但我认为它可以满足您的需求。
MATCH p=(n:Label1 {name:'main'})-[:calls*..2]->(m)
// 每个路径都将被返回
WITH COLLECT(p) AS ps, p, n
CALL apoc.convert.toTree(ps) YIELD value
// 作为嵌套文档返回节点n及其关系
RETURN {_id: ID(n), _type: labels(n), node:n, relationships: COLLECT(value['calls'][0])} AS result
示例结果:
[
  {
    "result": {
      "_id": 31,
      "_type": [
        "Label1"
      ],
      "node": {
        "name": "main"
      },
      "relationships": [
        {
          "calls.y": "prop y",
          "_type": "Main",
          "_id": 32
        },
        {
          "_type": "Main",
          "_id": 32,
          "calls.x": "prop x"
        }
      ]
    }
  }
]
希望这对您有帮助。
英文:
I offer a hack on this SO question. Although it is not exactly the resulting nested document as the apoc function but I think it can server your purpose.
MATCH p=(n:Label1 {name:'main'})-[:calls*..2]->(m)
// each path will be returned
WITH COLLECT(p) AS ps, p, n
CALL apoc.convert.toTree(ps) yield value
// return node n and it's relationships as a nested document
RETURN {_id: ID(n), _type: labels(n), node:n, relationships: collect(value['calls'][0])} as result
Sample result:
[
  {
    "result": {
      "_id": 31,
      "_type": [
        "Label1"
      ], 
      "node": {
        "name": "main"
      },
      "relationships": [
        {
          "calls.y": "prop y",
          "_type": "Main",
          "_id": 32
        },
        {
          "_type": "Main",
          "_id": 32,
          "calls.x": "prop x"
        }
      ]
    }
  }
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。




评论