Reputation: 395
I have a Cypher question. I have articles nodes with several properties. There is only one property value that I want to update in existing nodes.
For example I insert the node (a:ARTICLE {id:1, title:'test', score=2}
.
The score value change during the day so I want to keep the node in the database but only update the score.
For example I would like to keep the node above in the DB with same id and title but only score value updated: (a:ARTICLE {id:1, title:'test', score=3}
.
So I tried this:
To insert the articles in the database I do this:
"MERGE (a:ARTICLE {id:$id, title:$title}) SET a += {score:$score}"
But doesn't work the node is duplicated. How can I write well the request ?
Upvotes: 2
Views: 6756
Reputation: 6251
A simpler solution:
MATCH (a:ARTICLE {id:$id}) SET a.score = $score
Upvotes: 2
Reputation: 8950
First, you must determine what set of properties make the Article
nodes unique (side note: the PascalCase is more common for labels than UPPERCASE).
Let's assume id
is unique per article.
In that case, you only need to merge on that:
MERGE (article:Article {id: $id})
// [...]
It's usually a good idea to create an index as well, so that the MATCH
ing part of MERGE
executes fast. In that case, you would create an index for :Article(id)
.
Assuming the title never changes after the node creation:
MERGE (article:Article {id: $id})
ON CREATE SET article.title = $title
// [...]
Given the score always changes, you can then run a simple SET
clause without any ON CREATE
or ON UPDATE
prefix:
MERGE (article:Article {id: $id})
ON CREATE SET article.title = $title
SET article.score = $score
Of course, I made a few assumptions about your domain. Feel free to comment if I got some of them wrong and I'll update the answer.
Upvotes: 5
Reputation: 4052
Please check the type of $id, maybe it's a string when you execute the update query. Tried with your example Neo4j doesn’t create a new node.
Upvotes: 1