Add map as property in node or relation

Is there a way to add a map as a property? I want to save the address bar 1, 2, etc. Inside the address property itself.

something like this -

RETURN {address: {firstline:"a", secondline:"b"}, name:"ABC"} 

However, when I try to do this in CREATE or SET, it gives me an error.

I tried this -

 create (a:Person {name: "ABC", address: {firstline:"a", secondline:"b"}}) 

Error -

 Property values can only be of primitive types or arrays thereof 
0
source share
3 answers

You can add map as a property. not . You must either create Address as a separate node, or create a relationship using Person . Or add the address as a property array in Person , e.g..,

 (a:Person {name: "ABC", address: ["a", "b"]}) 

Of course, later you will have to convert this array somewhere to your application in order to get the map that you originally wanted.

+1
source

No, nested arrays are not supported, and this also makes no sense on the chart, because you usually model the address as a separate node from a person.

 (person:Person {name:"ABC"})-[:CURRENT_ADDRESS]->(addr:Address {firstLine:"a", secondLine:"b"}) 
+3
source

You can do this by serializing the nested map into a json string and clicking on node. If your requirement

  {address:{firstline:"a",secondline:"b"},name:"ABC"} 

Then iterate over the map, if the value is not a valid type in neo4j, then convert it to json string. those. you convert

  {firstline:"a",secondline:"b"}==>json string. 

In cypher, it acts like a normal string. SO works. When you retrieve data from a node, de-serialize the properties for your own objects.

The downside with this approach is that you need to de-serialize each property, because we do not know if the value is a normal string or is it a json string containing a nested map.

So, my solution for this is when you convert to json string, you specify which keys are converted to json, and store this information in node using a specific key, for example: when reading json_keys=['address'] only convert these keys to json_keys array.

 1)your map = {address: {firstline:"a", secondline:"b"}, name:"ABC"} 2)convert to json = {address: '{firstline:"a", secondline:"b"}', name:"ABC"} 3)note which keys are converted = {address: '{firstline:"a", secondline:"b"}', name:"ABC",json_keys=["address"]} 
0
source

Source: https://habr.com/ru/post/1202176/


All Articles