cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Nested query is returning an object instead of an array

My cypher is producing the following result:

{
  "path": "1",
  "childCount": 2,
  "indent": 0,
  "Node": {
    "name": "Root",
    "ID": "7e460fd4-92aa-41d5-8b1c-a5501b48dcdd"
  }
}

I think it needs to look like this:

{
  "path": "1",
  "childCount": 2,
  "indent": 0,
  "Node": [
    {
      "name": "Root",
      "ID": "7e460fd4-92aa-41d5-8b1c-a5501b48dcdd"
    },
  ]
}

What changes do I need to make to my query?

MATCH p = (:Node { name: "Root" })<-[:CHILD_OF *0..]-(c:Node) 
WITH c, apoc.text.join('1' + [rel in relationships(p) | rel.index], '.') as path, size((c)<-[:CHILD_OF]-()) as childCount, c as Node
ORDER BY path
RETURN  c {path, indent: size(split(path,'.'))-1, childCount, Node}

Any hints or help would be greatly appreciated.

4 REPLIES 4

intouch_vivek
Graph Steward

Neo4j is not equipped to store a Node as Node property.

https://neo4j.com/docs/cypher-manual/current/syntax/values/

They're using map projection to return a custom map derived from the node, and map values can be anything, even nodes or relationships, so this is valid.

Note that with your query, Node will only ever be a single value per row, it can never be multiple. c and Node are the same, c = Node, so per map projection from c, Node can only be c, singular, never multiple.

If you want to encapsulate that in a literal list (which will always and only have a single element), then you can handle that in your WITH projection when you aliased Node:

WITH ... , [c] as Node

Otherwise, if you're expecting that a list of nodes is possible, then you're missing out on some kind of aggregation, some collect() that isn't currently present in the query, but you'll need to articulate what you want collected and how.

I'm trying to fix the cypher query so I can run the following graphql query.

query {tree(root: "Root"){
	path
	indent
  childCount
  Node{
    name
    ID
  }
 }
}

The following error is returned.
"Expected to find a node at ' tree@378' but found Map{path -> String(\"1\"), childCount -> Long(2), indent -> Long(0), Node -> (109169)} instead",

109169 is the id of the node with the name "Root". I guess that the Node had to be in brackets because of other json examples I found.