Terence Chow
Terence Chow

Reputation: 11153

combining properties in cypher

I'd like to use cypher to return something of the form:

{
  name: 'Name of Parent Node',
  property1 : 'some property of parent node',
  property2 : 'some other property'
  children: [...some array of children...]
}

So far I've done the below:

MATCH (p:Parent)-[:SOME_RELATIONSHIP]->(c:Child)
WITH collect(c) as children, p
RETURN {properties: properties(p),  children: children} 

Which kinda is similar to what I want but not exactly. Is there a way to merge or combine it so that I get the properties together?

Upvotes: 0

Views: 1167

Answers (2)

Christophe Willemsen
Christophe Willemsen

Reputation: 20185

In Neo4j 3.1+ you can also use map projections (à la GraphQL)

MATCH (p:Parent)-[:SOME_RELATIONSHIP]->(c:Child)
RETURN p {.property1, .property2, children: collect(c)} AS info

You can also select only a couple of properties from the Child nodes :

MATCH (p:Parent)-[:SOME_RELATIONSHIP]->(c:Child)
RETURN p {.property1, .property2, 
          children: collect(c {.cprop1, .cprop2})
         } AS info

https://neo4j.com/blog/cypher-graphql-neo4j-3-1-preview/

Upvotes: 5

Tomaž Bratanič
Tomaž Bratanič

Reputation: 6514

If you always return same properties from parent Node

MATCH (p:Parent)-[:SOME_RELATIONSHIP]->(c:Children) 
WITH collect(c) as children,p
return p.name as name,p.firstproperty,p.secondproperty,children

Upvotes: 0

Related Questions