I am new in neo4j and I am having problems with the MERGE clause. I have a method to do queries like this:
def upsert (idValue, valueField1):
query = "MERGE (n:Node {id: '" + idValue+ "'}) "
query += "ON MATCH SET n.field1= n.field1+ " + str(valueField1) + " "
query += "ON CREATE SET n = {id: '" + idValue + "', field1: 0} "
return db.run(query)
Then, I am calling the method like this:
upsert("1", 0)
upsert("2", 0)
upsert("3", 5)
upsert("1", 2)
upsert("1", 1)
So, afterthat, I expected this:
node (id="1", field1=3)
node (id="2", field1=0)
node (id="3", field1=0)
But, I am getting this:
node (id="1", field1=2)
node (id="2", field1=0)
node (id="3", field1=0)
In addition, If I do the same calls again, I got this:
node (id="1", field1=4)
node (id="2", field1=0)
node (id="3", field1=5)
Could anyone explain me what is happening and what am I doing wrong please? I was looking for on internet, but I couldn't find anything helpful for me.
With this query, that is exactly the same as what you are doing, I have the good result :
And then with
MATCH (n:Node) RETURN n.id, n.field1 ORDER BY n.id LIMIT 25
:So I don't think that the problem comes from the query itself, but from its construction.
You should consider to use a parameterized query. It will be easier to read the query, but also code it and it's more performant in Neo4j. So your code should be :