Weird Neo4J Cypher behavior on setting relationshi

2019-08-27 21:22发布

问题:

I have a Cypher request for Neo4J of this kind:

MATCH (u:User {uid: $userId}) 
UNWIND $contextNames as contextName 
MERGE (context:Context {name:contextName.name,by:u.uid,uid:contextName.uid}) 
ON CREATE SET context.timestamp=$timestamp 
MERGE (context)-[by:BY]->(u) 
SET by.timestamp = $timestamp 

My params are:

{
 "userId": "15229100-b20e-11e3-80d3-6150cb20a1b9",
 "contextNames": [
  {
    "uid": "822e2580-1f5e-11e9-9ed0-5b93e8900a78",
    "name": "fnas"
  }
 ],
 "timestamp": "1912811921129"
}

That query above sets 8 parameters because I have (probably) 8 other relationships of the :BY type in relation to that u. Which seems to me illogical as it should only find one relationship between a concrete context and a concrete u, create it if it doesn't exist, and set the property for it

However, when I do this:

 MATCH (u:User {uid: $userId}) 
 UNWIND $contextNames as contextName 
 MERGE (context:Context {name:contextName.name,by:u.uid,uid:contextName.uid}) 
 ON CREATE SET context.timestamp=$timestamp 
 MERGE (context)-[:BY{timestamp:$timestamp}]->(u)

It either creates a relationship (if the one with the same timestamp doesn't exist) or it simply doesn't do anything (which seems to be the right behavior).

What is the reason for this discrepancy? A bug in Neo4J?

标签: neo4j cypher