Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
07-06-2022 09:29 AM - edited 07-06-2022 09:52 AM
I am trying to use the value of a node property as a variable name for a relationship property, and I haven't found any way to do it so far. Here is an example:
Let's say I have some node labels including:
(p:Person {id: #, name: "some name"})
(c:Currency {id: #, name: "currency symbol"})
(t:Transaction {id: #, amount: #}
In this case, the transaction is not linked to the currency, but ignore this for now. I want to create a relationship to show that some person transacted with another.
Assumptions:
1. c.name can be one of many currencies [USD, EUR, GBP, ... etc]
2. If person A sends multiple currencies to person B, then each currency will have its own transaction.
Now if p1 sent some amounts of USD and EUR to p2, I want to create a relationship as follows:
(p1:Person)-[TRANSACTED_WITH {USD: some amount, EUR: some amount}]->(p2:Person)
So I want the relationship to have the symbol name of a sent currency as the property name and the amount as the value of that property. There can be one or multiple currencies sent from A to B. So each relationship between two persons can have a different set of properties.
I tried the following, but it didn't work:
MERGE (p1:Person)-[TRANSACTED_WITH {c1["name"]: t1.amount}]->(p2:Person)
Is there actually any way to do that?
07-06-2022 03:01 PM
Property names must be literal values. You can try using apoc doIt procedure, as it will let you execution a cypher statement passed as a string. As such, you can create your cypher string with the relationship's property name substituted with the value you need.
https://neo4j.com/labs/apoc/4.2/overview/apoc.cypher/apoc.cypher.doIt/
07-07-2022 03:47 AM
Hi @TK36 !
As @glilienfield said, there're several APOC procedures that may help you with this. An example of this can be:
MATCH(p1:Person { id : $id1}), (t1:Transaction { id : $id2}), (c1:Currency {id : $id3}), (p2:Person { id : $id4})
with *
CALL apoc.create.relationship(p1, 'TRANSACTION',
apoc.map.fromValues([
c1.name, t1.amount
]), p2)
YIELD rel
RETURN rel;
All the sessions of the conference are now available online