Kevin
Kevin

Reputation: 313

Cypher-Neo4j Node Single Property change to Array

Following is a Node we having in DB

P:Person { name:"xxx", skill:"Java" }

and after awhile, we would like to change the Skill to skill array, is it possible?

P:Person { name:"xxx", skill:["Java", "Javascript"] }

Which Cypher query should I use?

Upvotes: 1

Views: 712

Answers (2)

Supamiu
Supamiu

Reputation: 8731

In fact, I think your real problem here is not how to get an array property in a node, but how to store it. Your data model is wrong in my opinion, storign data as array in neo4j is not common, since you have relations to store multiple skills (in your example).

How to create your data model

With your question, I can already see that you have one User, and one User can have 1..n skills.

I guess that one day (maybe tomorrow) you will need to know which users are able to use Java, C++, PHP, and every othre skills.

So, Here you can already see that every skill should have its own node.

What is the correct model in this case?

I think that, still with only what you said in question, you should have something like this:

(:Person{name:"Foo"})-[:KNOWS]->(:Skill{name:"Bar"})

using such a data model, you can get every Skill known by a Person using this query:

MATCH (:Person{name:"Foo"})-[:KNOWS]->(skill:Skill)
RETURN skill //or skill.name if you just want the name

and you can also get every Person who knows a Skill using this:

MATCH (:Skill{name:"Bar"})<-[:KNOWS]-(person)
RETURN person //Or person.name if you just want the name

Keep in mind

  • Storing array values in properties should be the last option when you are using neo4j.
  • If a property can be found in multiple nodes, having the same value, you can create a node to store it, then you will be able to link it the other nodes using relations, and finding every node having the property X = Y will be easier.

Upvotes: 1

Luanne
Luanne

Reputation: 19373

If you have a single skill value in skill, then just do

MATCH (p:Person)
WHERE HAS (p.skill)
SET p.skill=[p.skill]

If there are multiple values you need to convert to an array such as P:Person { name:"xxx", skill:"Java","JavaScript" } then this should work:

MATCH (p:P)
SET p.skill= split(p.skill,",")

Upvotes: 2

Related Questions