Search code examples
neo4jneo4j-ogm

how to add an attribute before returning the node in neo4j


I need to return a collection as an attribute of a node. I have a person node that can have several relationships with another person.

enter image description here

I need to return all the people that follow a person A and return all the relationships between them as an attribute,

this is the query:

match (a: Person {email:' a@email.com '}) <- [: FOLLOW] - (x: Person)
with a, x
match (a) - [r] - (x) 
return x, collect (type (r)) as relations; 

I need to add to the person x, the collection 'relations' as an attribute of the node 'x'

It is just the opposite to what is explained here: 'https://neo4j.com/developer/kb/updating-a-node-but-returning-its-state-from-before-the-update/', in this case, they return a snapshot but before they update the node, what I really need is to modify the snapshot without actually updating the node, I'm trying:

match(a:Person{email:'a@email.com'})<-[:FOLLOW]-(x:Person) 
with a,x
match(a)-[r]-(x)
with properties(x) as snapshot, collect(type(r)) as relations;
set snapshot.relations = relations
RETURN snapshot

but when I did it gave me this error: Expected exactly one statement per query but got: 2

UPDATE: as well said @krishna-reddy eliminating the ';' Fixes the above error, but now it shows this: Neo.ClientError.Statement.SyntaxError: Type mismatch: expected Node or Relationship but was Map


Solution

  • You can use the APOC plugin which has procedures for creating Virtual Nodes and Relationships which are not stored in the database.

    Virtual Nodes and Relationships don’t exist in the graph, they are only returned to the UI/user for representing a graph projection. They can be visualized or processed otherwise.

    MATCH(a:Person{email:'a@email.com'})<-[:FOLLOW]-(x:Person) 
    WITH a,x
    MATCH(a)-[r]-(x)
    WITH x, collect(type(r)) AS relations
    CALL apoc.create.vNode([head(labels(x))], x{.*,relations:relations}) YIELD node AS snapshot
    RETURN snapshot