Nested Maps and Collections in Neo4j 2
Asked Answered
T

3

6

I understand that node and relationship properties are limited to primitive types or arrays of primitive types. The "Maps" section of the Neo4j 2.1 Reference Card mentions that:

{name:'Alice', age:38, address:{city:'London', residential:true}}

Literal maps are declared in curly braces much like property maps. Nested maps and collections are supported.

Of course something like:

CREATE (alice {name:'Alice', age:38, address:{city:'London', residential:true}})

throws an exception:

Error: Property values can only be of primitive types or arrays thereof Neo.ClientError.Statement.InvalidType

In what context does Neo4j support nested maps and collections?

Thirty answered 9/9, 2014 at 16:49 Comment(0)
P
4

(Edited)

The reference card is a bit too subtle. The most important word is "literal". In other words, you can only use arbitrarily nested maps and arrays in literals, but you cannot store such things in a node or relationship.

For example, this works:

WITH {name:'Alice', age:38, address:[{city:'London', residential:true}, {city: 'Paris', residential: false} ]} AS x
RETURN x;

But this fails:

CREATE (x {name:'Alice', age:38, address:[{city:'London', residential:true}, {city: 'Paris', residential: false} ]})
RETURN x;
Protasis answered 9/9, 2014 at 22:37 Comment(1)
so how to store them?Fusionism
B
0

There is a way to do this by converting nested maps to json string. Answered here

Butane answered 30/7, 2017 at 10:44 Comment(0)
L
0

There is also a trick You can do to put a map of properties in the node like this

CREATE (x {
    `properties.name`: 'Alice', `properties.city`: 'London', `properties.residential`: true }
  )

Then in Your entity use the @CompositeProperty:

@CompositeProperty(converter = PropertiesToMapConverter.class)
    private Map<String, Object> properties;

and a converter that will convert needed types to those stored at graph DB.

Lodie answered 21/1, 2022 at 7:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.