Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
03-15-2021 02:15 AM
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
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?
03-15-2021 04:11 AM
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
03-15-2021 06:26 AM
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?
03-15-2021 08:10 AM
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.
All the sessions of the conference are now available online