Aerodynamika
Aerodynamika

Reputation: 8413

How to change the same property for several nodes in Neo4J?

I have the following Cypher Neo4J query, where I'm trying to find 3 nodes with a certain property and rename them:

MATCH (u:User{uid:"418938923891"}), 
(ctx:Context{name:"seo_191220T1718"}), (ctx)-[:BY]->(u), 
(ctxk:Context{name:"kwrds_191220T1718"}), (ctxk)-[:BY]->(u), 
(ctxs:Context{name:"serp_191220T1718"}), (ctxs)-[:BY]->(u)  
WITH DISTINCT ctx, ctxk, ctxs 
SET ctx.name = "seo_storyn", 
ctxk.name = "kwrds_storyn", 
ctxs.name = "serp_storyn";

All works fine, however, when the query runs it says 24 properties changed, probably because the WITH results are multiplied.

Is there a more elegant and efficient way to do that?

Upvotes: 1

Views: 60

Answers (1)

Nathan Smith
Nathan Smith

Reputation: 881

This streamlined version of the query should update the same nodes that yours does. Is it possible that you have more than one node matching the name or uid values you searched for?

MATCH 
(ctx:Context {name:"seo_191220T1718"})-[:BY]->(u:User {uid:"418938923891"}),
(ctxk:Context {name:"kwrds_191220T1718"})-[:BY]->(u),
(ctxs:Context {name:"serp_191220T1718"})-[:BY]->(u)
SET ctx.name = "seo_storyn", 
ctxk.name = "kwrds_storyn", 
ctxs.name = "serp_storyn"
RETURN u, ctx, ctxk, ctxs

I added a return clause so that you can see which patterns the query is matching. Try running it in neo4j browser, and then looking at the results in table view. That might give you an idea of why you are getting multiple results.

You could also break the query into individual parts and see whether multiple results are returned.

MATCH (ctx:Context {name:"seo_191220T1718"})-[:BY]->(u:User {uid:"418938923891"}) RETURN *
MATCH (ctxk:Context {name:"kwrds_191220T1718"})-[:BY]->(u:User {uid:"418938923891"}) RETURN *
MATCH (ctxs:Context {name:"serp_191220T1718"})-[:BY]->(u:User {uid:"418938923891"}) RETURN *

Upvotes: 1

Related Questions