Add a map as property in node or relationship

こ雲淡風輕ζ 提交于 2020-01-06 15:10:13

问题


Is there a way to add a map as property? I want to store address line 1, 2 etc inside the address property itself.

something similar to this -

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

However when I try this in CREATE or SET it gives me 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

回答1:


It is not possible to add map as a property. You should either create Address as a separate node and create a relationship with the Person. Or add the address as a property array to the Person, eg.,

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

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




回答2:


No, nested arrays are not supported, also in a graph it doesn't make sense because you would generally model the Address as a distinct node from the person.

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



回答3:


You can do this by serializing a nested map to JSON string and push to the node. If your requirement is

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

Then iterate through the map if the value is not a valid type in neo4j then convert it to JSON string. ie you convert

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

In cipher it acts as a normal string, SO it works. When you retrieve data from node de-serialize properties to native objects.

The downside with this approach is that you need to de-serialize every property because we don't know if a value is a normal string or it is a JSON string containing nested map.

So my solution for this is, when you are converting to JSON string you note which keys are being converted into JSON and keep this information in the node with a specific key eg: json_keys=['address'] when reading only convert those keys in 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"]}


来源:https://stackoverflow.com/questions/37476949/add-a-map-as-property-in-node-or-relationship

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!