Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert array of objects as property into neo4j

I am trying to insert array of objects as property to node. I tried

MERGE (ss:Label1 {sId: 12345})
  ON MATCH SET ss.id = 14770746012, ss.setC = 1,ss.nl = [{id: 24, status: 0}]
  ON CREATE SET ss.id = 14770746012, ss.setC = 1,ss.nl = [{id: 24, status: 0}]

If run I am getting follwing error::

Property values can only be of primitive types or arrays thereof

What to even I tried with nested array, that gave me same error as above.

I studied in Neo4j documentation that Neo4j can't support 'Nesting of property values'

How to achieve my requirement?

like image 485
chikku Avatar asked Oct 25 '16 10:10

chikku


2 Answers

As Neo4j not support hierarchical properties, one of the ways to solve this problem - to create additional nodes:

MERGE (ss:Label1 {sId: 12345, id: 14770746012, setC: 1 })
MERGE (nl:Props:nlProp {id: 24, status: 0})
MERGE (ss)-[:hasProps]->(nl)
like image 161
stdob-- Avatar answered Sep 28 '22 00:09

stdob--


As mentioned by stdob--, you can create additional nodes if your need to use those properties in queries (for filtering, aggregation, etc.).

If however, you just want to write and read the data but not act on it in queries, you can serialize it, for example as JSON:

MERGE (ss:Label1 {sId: 12345})
  ON MATCH SET ss.id = 14770746012, ss.setC = 1,ss.nl = ["{\"id\": 24, \"status\": 0}"]
  ON CREATE SET ss.id = 14770746012, ss.setC = 1,ss.nl = ["{\"id\": 24, \"status\": 0}"]
like image 41
Frank Pavageau Avatar answered Sep 27 '22 23:09

Frank Pavageau