Have difficult CYPHER query about collection using neoism golang?

70 Views Asked by At

I build a social network, when a comment include mentions (like Facebook), I send a comment json to webservice:

{
  "owner": 5,                // id of user write comment
  "message": "text",
  "mentions":[
    {
      "id": 1,               // user id
      "name": "mention",     // name is shown
      "offset": 0,           // position of start of mention text
      "length": 10           // length of mention text
    },
    {
      "id": 2,               // user id
      "name": "mention",     // name is shown
      "offset": 0,           // position of start of mention text
      "length": 10           // length of mention text
    }
  ],
}

DATA MODEL: (User)-[WRITE]->(Comment{message})-[MENTION{name,offset,length}]->(User)

In source code, I use below code to return res with ID of comment

cq := neoism.CypherQuery{
        Statement:  stmt,
        Parameters: params,
        Result:     &res,
    }

My problem is I don't know how to write query for this. Too hard for me.

1

There are 1 best solutions below

0
On BEST ANSWER

Here is how to do the Cypher without breaking apart the json first.

WITH {json} AS map
MATCH (u:User)
WHERE id(u)=map.owner
CREATE (u)-[:WRITE]->(c:Comment{message:map.message})
FOREACH (mention IN map.mentions | 
    MATCH (u2:User) WHERE id(u2) = mention.id 
    CREATE (c)-[:MENTION{name:mention.name, length:mention.length, offset:mention.offset}]->(u2))
RETURN id(c) AS id