Benjamin McFerren
Benjamin McFerren

Reputation: 862

Cypher query with literal map syntax & dynamic keys

I'd like to make a cypher query that generates a specific json output. Part of this output includes an object with a dynamic amount of keys relative to the children of a parent node:

{
   ...

   "parent_keystring"                 : {
           child_node_one.name        : child_node_one.foo
           child_node_two.name        : child_node_two.foo       
           child_node_three.name      : child_node_three.foo
           child_node_four.name       : child_node_four.foo
           child_node_five.name       : child_node_five.foo
   }
}

I've tried to create a cypher query but I do not believe I am close to achieving the desired output mentioned above:

MATCH (n)-[relone:SPECIFIC_RELATIONSHIP]->(child_node)
WHERE n.id='839930493049039430'
RETURN n.id          AS id,
       n.name        AS name,
       labels(n)[0]  AS type,
       {
           COLLECT({ 
              child.name : children.foo
           }) AS rel_two_representation
       } AS parent_keystring

I had planned for children.foo to be a count of how many occurrences of each particular relationship/child of the parent. Is there a way to make use of the reduce function? Where a report would generate based on analyzing the array proposed below? ie report would be a json object where each key is a distinct RELATIONSHIP and the property value would be the amount of times that relationship stems from the parent node?

Thank you greatly in advance for guidance you can offer.

Upvotes: 3

Views: 1791

Answers (2)

FylmTM
FylmTM

Reputation: 1997

I think, Neo4j Server API output by itself should be considered as any database output (like MySQL). Even if it is possible to achieve, with default functionality, desired output - it is not natural way for database.

Probably you should look into creating your own server plugin. This allows you to implement any custom logic, with desired output.

Upvotes: 0

Brian Underwood
Brian Underwood

Reputation: 10856

I'm not sure that Cypher will let you use a variable to determine an object's key. Would using an Array work for you?

COLLECT([child.name, children.foo]) AS rel_two_representation

Upvotes: 1

Related Questions