MichaelE
MichaelE

Reputation: 767

I want to create and manipulate a property array in Neo4j

I want to create and manipulate a property array in Neo4j. I am new to both Graph Database and Neo4j. I am building a membership graph database with a subscription component. I need the member to relate to a LEDGER node with an array property for each month of subscription paid. I know I can create a node for each payment made...but I am trying to neatly assign the member subscription to one node. From what I understand so far I cannot have a Key/Value pair in the Array Property...but can I assign the values to the property array with:

SET Ledger.AmtPaid[index]=amt or something similar...I get errors when I try this:

CREATE (m:Member {name: "Michael"})
CREATE (l:Ledger {name: "Member Ledger", AmtPaid :[]})
CREATE (m)-[:PAID_INTO]->(l)

MATCH (m {name:"Michael"})--(l)
SET l.AmtPaid[0]="50"
Return l.AmtPaid[0];

Upvotes: 2

Views: 3123

Answers (1)

Michael Hunger
Michael Hunger

Reputation: 41676

Don't forget to use labels and rel-types in your query

Unfortunately writing to an index doesn't work as Cypher treats its collections as immutable.

You can either do:

MATCH (m:Member {name:"Michael"})-[:PAID_INTO]->(l)
SET l.AmtPaid = ["50"] + l.AmtPaid[1..]
Return l.AmtPaid[0];

or

MATCH (m:Member {name:"Michael"})-[:PAID_INTO]->(l)
SET l.AmtPaid = l.AmtPaid + ["50"]
Return l.AmtPaid[0];

or

MATCH (m:Member {name:"Michael"})-[:PAID_INTO]->(l)
SET l.AmtPaid = 
   reduce(a=[], idx in range(0,size(l.AmtPaid) | 
          a + case idx when 0 then ["50"] else [l.AmtPaid[idx]] end)
Return l.AmtPaid[0];

Upvotes: 2

Related Questions