ottonormal
ottonormal

Reputation: 114

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 :).

Upvotes: 2

Views: 365

Answers (2)

cybersam
cybersam

Reputation: 66999

  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)
    

Upvotes: 3

Ontologiae
Ontologiae

Reputation: 605

Merge is used when you want to create if the relationship doesn't exists, and refresh it otherwise. So, ON MERGE SET would be enough.

So,

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 MATCH SET 
        b.timestamp = request.timestamp
        

should works

Upvotes: 0

Related Questions