0
votes

I'm creating an app using GRANDstack on top of a Neo4j graph database and I'm struggling with writing a query that unwinds a property array, matches some IDs, pullsback names associated with the IDs and repackages them into a new array.

Essentially, I have Memory nodes (Mems) and Person nodes (Person) and their relationship is as follows: (Mems)-[WITH]->(Person). i.e. you can have a memory of something where you were WITH multiple people. The Mems nodes contain a personID property that is an array of IDs and the relationship in the graph database is built from the array of personIDs in Mems nodes that match the personIDs in the Person nodes.

The result I'm trying to achieve is like the below, however I haven't found a way to return the nicknames from the Person nodes:

enter image description here

The query I'm using to generate the above is below:

MATCH (m:Mem)-[WITH]->(p:Person)
WHERE m.personID IS NOT NULL
UNWIND m.personID as personID
CALL {
    WITH personID
    MATCH (p:Person)
    WHERE personID = p.personID
    RETURN p.nickname
}
RETURN m.mem, m.date, personID, p.nickname

I think it's close to what I need but I just can't figure out the last bit where I can return the nicknames associated with the personIDs. This might be something that APOC is more capable of doing?

1

1 Answers

1
votes

[UPDATED]

This should work for you:

MATCH (m:Mem)-[:WITH]->(p:Person)
WHERE p.personID IN m.personID
RETURN m.mem, m.date, p.personID, p.nickname

Also, notice that the relationship pattern must use:WITH instead of WITH if you want to query for the WITH relationship type.

Addendum

Of course, since you have the WITH relationships, you should consider deleting the redundant m.personID property. You should get the same results without m.personID this way:

MATCH (m:Mem)-[:WITH]->(p:Person)
RETURN m.mem, m.date, p.personID, p.nickname