Search code examples
neo4jcyphernodes

Neo4j: add properties to a relation on match and on create


My Skript is watching to a folder and will run Cypherqueries if there are any changes.
I try to create a relation between two nodes i created before. When a relation will be created, it should have a "Create Timestamp" and if the node already exist, the relation should update a "Update Timestamp". Here is what i try:

CALL apoc.load.json("path/to/my/JSON") yield value 
    WITH value.`Request`.timestamp AS request
    MATCH(a:foo), (c:bar)
    WHERE a.id = c.id
    MERGE(a)-[b:has_relation]->(c)
    ON CREATE SET 
        b += {
            creation_batch_timestamp:trim(request.timestamp)
            }
    ON MATCH SET 
        b += {
            update_batch_timestamp:trim(request.timestamp)
            }

What i get back, is a Type mismatch error "expected a map but was a String" (my timestamp is a String)

Is it possible to use a MERGE-Statement for creating or update a relation? How i can solve this? Thanks for helping :).


Solution

    1. The request value is probably already the desired timestamp string.

      Here is one solution. Change this:

      WITH value.`Request`.timestamp AS request
      

      to this:

      WITH value.Request AS request
      
    2. It would also be clearer to change this:

      ON CREATE SET 
        b += {
          creation_batch_timestamp:trim(request.timestamp)
          }
      ON MATCH SET 
        b += {
          update_batch_timestamp:trim(request.timestamp)
          }
      

      to this:

      ON CREATE SET b.creation_batch_timestamp = trim(request.timestamp)
      ON MATCH  SET b.update_batch_timestamp = trim(request.timestamp)