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.

Spread operator for map projections?

BairDev
Node Clone

Just in case I've missed it in the docs: is there something like spread operators for map projections?

I have two related nodes, which I would get with MATCH (b:Building)--(a:Address).

Now I can project the address into the building with RETURN b{.*, id: ID(b), address: a} AS building;

But is it possible to flatten the map and add the properties of Address to the new map without referencing them one by one?

We could have apoc.create.virtual.fromNode(node :: NODE?, propertyNames :: LIST? OF STRING?) (from docs) for multiple nodes, maybe with some property configuration like it is supposed to work in nodes.collapse (which seems to live in a completely different domain).

Or we could have some spread operator like RETURN b{.*, id: ID(b), ..a} AS building;.

1 ACCEPTED SOLUTION

If that does not work for you, and you really want a flat map, apoc has some procedures:

Merge two maps (just merges the key/value pairs):

with b{.*, id: id(b)} as bMap, properties(a) as aMap
return apoc.map.merge( bMap,  aMap)

Flatten a map (uses dot notation to merge key/values):

with b{.*, id: id(b), address: properties(a)} as map
return apoc.map.flatten( map)

or, if you want each node to be prefixed:

with {id: id(b), building: properties(b),  address: properties(a)} as map
return apoc.map.flatten( map)

View solution in original post

3 REPLIES 3

Sure, try:

RETURN b{.*, id: ID(b), address: properties(a)}

If that does not work for you, and you really want a flat map, apoc has some procedures:

Merge two maps (just merges the key/value pairs):

with b{.*, id: id(b)} as bMap, properties(a) as aMap
return apoc.map.merge( bMap,  aMap)

Flatten a map (uses dot notation to merge key/values):

with b{.*, id: id(b), address: properties(a)} as map
return apoc.map.flatten( map)

or, if you want each node to be prefixed:

with {id: id(b), building: properties(b),  address: properties(a)} as map
return apoc.map.flatten( map)

Ah, map.merge and map.flatten do look promising. Thanks!

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

All the sessions of the conference are now available online