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.

SET and REMOVE node attributes in one statement

I am now into my second hour of experimenting with Neo4J. Thus far, I am very very impressed with what I see. The tutorials, the Windows desktop version, the syntax of Cypher... are pretty much perfect. However, I am finding it difficult to understand just how I should make multiple changes to a node in one statement.

For the present purpose I am simply working with the default Movies database. Suppose I want to do the following

  1. Create a Person with an additional role of "Alien"
  2. Give that instance of Person the additional attribute of Peau = 'Green'
  3. Change my mind and decide that the attribute name/value pair should be in English rather than French
  4. So at this stage I want to REMOVE peau and SET skin.

Not a problem in itself if I use the following sequence

CREATE (p:Person:Alien {name:'ET'});
MATCH(a:Alien) RETURN a;
//all good Person with the extra role of fAlien now exists
MATCH(a:Alien) SET a.born = 1987 RETURN s;
//still good
MATCH(a:Alien) SET a.peau = 'Verte' RETURN a;
//our Alien now has a green skin - in French
MATCH(a:Alien) SET a.skin = 'Green' RETURN a;
//our Alien now also has a green skin in English
MATCH(a:Alien) REMOVE a.peau RETURN a;
//all good, stripped out that unrequired French peau attribute

While this works it does seem like unnecessary extra work.  Surely, I should be able to SET and REMOVE in one statement.  However,

MATCH(a:Alien)  SET a.skin = 'Green' REMOVE a.peau = 'Verte' RETURN a;

Yes, that worked too. However, with my pgSQL background this feels a bit odd to me. Put in a comma before the REMOVE and Cypher throws up an error because it does not understand - it thinks I am attempting to continue the prior SET... . Is the way I have written the statement the only valid way to accomplish what I need here?

3 REPLIES 3

You can do it all in one statement, like this:

CREATE (a:Person:Alien {name:'ET'})

//all good Person with the extra role of fAlien now exists
SET a.born = 1987
//still good
SET a.peau = 'Verte'
//our Alien now has a green skin - in French
SET a.skin = 'Green'
//our Alien now also has a green skin in English
REMOVE a.peau RETURN a;
//all good, stripped out that unrequired French peau attribute

Thanks. To put my question another way - when does one use commas in Cypher. From my understanding thus far only to separate node attributes/properties. When issuing Cypher commands - e.g. SET, DELETE etc - you do not. Presumably the freshly issued command, e.g, DELETE, "closes the previous command, e.g. SET.

Is my understanding correct?

You can use the commas when you're creating stuff. e.g.

CREATE (:Person {id: 1}), (:Person {id: 2})

And then you can also use them when you're trying to find things:

MATCH (p1:Person {id: 1}), (p2:Person {id: 2})

On and also if you wanted to set multiple properties:

MATCH (p1:Person {id: 1})
SET p1.time = time(), p1.date = date()
RETURN p1

And then the ; ends a statement.