how to add new property to an existing relationship?

1,104 views
Skip to first unread message

kincheong lau

unread,
Mar 23, 2016, 3:36:01 AM3/23/16
to Neo4j
for each existing 'product' that 'inside' a 'category', I would like to add a property in relationship 'product->category'. When I run below cypher, it create a new relationship instead of adding new property in the existing one...how can I make it 1 relationship only?


1) Existing relationship before change

(p:Product)-[r:inside]->(c.Category)

there is no property in [ r ]

2) Cypher to run

MATCH (p:Product), (c:Category)
MERGE (p)-[r:inside {type: p.id + '->' + c.id }]->(c)

3) it creates new relationship and the existing no change

(p:Product)-[r:inside {type: p.id->c.id }]->(c:Category)

(p:Product)-[r:inside]->(c:Category)




Michael Hunger

unread,
Mar 23, 2016, 3:53:31 AM3/23/16
to ne...@googlegroups.com
MATCH (p:Product)-[r:inside]->(c:Category) 
SET r.type = p.id + '->' + c.id

But I strongly recommend against setting this kind of property !!

As you can always compute the information!

MATCH (p:Product)-[r:inside]->(c:Category) 
RETURN p.id + '->' + c.id


MERGE always tries to find the WHOLE pattern and if it can't find it will CREATE it.

--
You received this message because you are subscribed to the Google Groups "Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

kincheong lau

unread,
Mar 23, 2016, 4:49:56 AM3/23/16
to Neo4j
Thanks!  I was thinking adding unique property and then create index/constraint might improve performance, would that be the case?

Michael Hunger

unread,
Mar 23, 2016, 5:31:09 AM3/23/16
to ne...@googlegroups.com
No, there are no constraints on rels.

What's your use-case? Do you have an actual performance problem?

Michael

Bikash Karmokar

unread,
Jan 2, 2017, 5:40:11 AM1/2/17
to Neo4j
thanks a lot. 
Reply all
Reply to author
Forward
0 new messages