cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Change value in array

lx2pwnd
Node Clone

I have some nodes in my db which have a label like this

"graph_information": [
    "prova",
    "password"
  ]

I want to change the values inside this array, but I can't to do it.

I tried in this way

MATCH (n) 
WHERE 
      ANY(gi IN n.graph_information WHERE gi = 'prova')
set  n.graph_information_new = ['prova2',  n.graph_information[1] ]
remove n.graph_information
set n.graph_information = n.graph_information_new
remove n.graph_information_new

And it seems to work, but is there a better method to do this ?

1 ACCEPTED SOLUTION

Hi,

This is the test data.

CREATE (:SomeLabel {graph_information: ['prova', 'password']}),
       (:SomeLabel {graph_information: ['prova', 'prova']}),
       (:SomeLabel {graph_information: ['user1', 'prova']})

You can use ONE SET sentence like this.
But in your Where clause, I think the result is wrong.

MATCH (n)
  WHERE
  any(gi IN n.graph_information WHERE gi = 'prova')
SET  n.graph_information = ['prova2', n.graph_information[1] ]
RETURN n.graph_information
n.graph_information
["prova2", "password"] <- OK
["prova2", "prova"] <- OK
["prova2", "prova"] <- The ID 'user1' has been changed.

I think 'prova' is an id, not a password, so it's better to specify it when searching.

MATCH (n)
  WHERE n.graph_information[0] = 'prova'
SET  n.graph_information = ['prova2', n.graph_information[1] ]
RETURN n.graph_information
["prova2", "password"] <- OK
["prova2", "prova"] <- OK

OR

MATCH (n)
  WHERE n.graph_information = ['prova', 'password']
SET  n.graph_information = ['prova2', n.graph_information[1] ]
RETURN n.graph_information
["prova2", "password"] <- OK

If you have a large number of records, you need an Index, so I think it's better to set the id and password as separate items from the array.

View solution in original post

2 REPLIES 2

Hi,

This is the test data.

CREATE (:SomeLabel {graph_information: ['prova', 'password']}),
       (:SomeLabel {graph_information: ['prova', 'prova']}),
       (:SomeLabel {graph_information: ['user1', 'prova']})

You can use ONE SET sentence like this.
But in your Where clause, I think the result is wrong.

MATCH (n)
  WHERE
  any(gi IN n.graph_information WHERE gi = 'prova')
SET  n.graph_information = ['prova2', n.graph_information[1] ]
RETURN n.graph_information
n.graph_information
["prova2", "password"] <- OK
["prova2", "prova"] <- OK
["prova2", "prova"] <- The ID 'user1' has been changed.

I think 'prova' is an id, not a password, so it's better to specify it when searching.

MATCH (n)
  WHERE n.graph_information[0] = 'prova'
SET  n.graph_information = ['prova2', n.graph_information[1] ]
RETURN n.graph_information
["prova2", "password"] <- OK
["prova2", "prova"] <- OK

OR

MATCH (n)
  WHERE n.graph_information = ['prova', 'password']
SET  n.graph_information = ['prova2', n.graph_information[1] ]
RETURN n.graph_information
["prova2", "password"] <- OK

If you have a large number of records, you need an Index, so I think it's better to set the id and password as separate items from the array.

Thank you @koji . It works perfectly