Return list of node and property values

I want to retrieve a list of all distinct values of node and property values from a database, a list that I can copy-paste easily without any double values. So, for example, from the database below, my ideal return would be a list:

"Master, MSc., Master of Science, Ecology, Ecosystem, Biodiversity, Inclusiveness, Europe"

//Create dataset and relations
CREATE (a:Term {Name:"Ecology", Synonyms:"Ecosystem, Biodiversity, Inclusiveness"});
CREATE (b:Degree {Name:"Master", Synonyms:"MSc., Master of Science"});
CREATE (c:Location {Name:"Europe"});
MATCH (b:Term {Name:"Ecology"}), (a:Degree {Name:"Master"}) MERGE (a)-[:PART_OF]->(b);
MATCH (a:Term {Name:"Ecology"}), (b:Location {Name:"Europe"}) MERGE (b)-[:IS_LOCATION]->(a);

MATCH (b:Degree)-[:PART_OF]->(a:Term)<-[:IS_LOCATION]-(c:Location)
RETURN b.Name, b.Synonyms, a.Name, a.Synonyms, c.Name, c.Synonyms; //This returns a table in the return area of the browser, but has a lot of doubles and I can't copy-paste it straight away, so this is not ideal

You could do it like this:

MATCH (n)
WITH [key in keys(n) WHERE not(n[key] is null)| n[key]] AS values
UNWIND values AS value
WITH DISTINCT value
RETURN apoc.text.join(collect(value), ", ")

1 Like

Ola Mark,

That is the output I need! Great, thank you. But now, I don't want to select the whole database, but I only want a subset, which I select with (suppose I have more Location nodes):

MATCH (b:Degree)-[:PART_OF]->(a:Term)<-[:IS_LOCATION]-(c:Location {Name:"Europe"})

Do you know of a way that I can integrate that in the query you proposed?

You can update the first line like this:

MATCH path = (b:Degree)-[:PART_OF]->(a:Term)<-[:IS_LOCATION]-(c:Location {Name:"Europe"})
UNWIND nodes(path) AS n
WITH [key in keys(n) WHERE not(n[key] is null)| n[key]] AS values
UNWIND values AS value
WITH DISTINCT value
RETURN apoc.text.join(collect(value), ", ")

Works like a charm, thank you :)

I am trying to accomplish the same goal, so I tried your solution. I am receiving errors that appear to come from datatypes in the property values

The first error I saw was:

Neo.ClientError.Statement.TypeError

Can't coerce 2024-04-02T20:06:00Z to String

So, I found the one node where I use the nested date/time format, and then I saw this error:

Neo.ClientError.Statement.TypeError

Can't coerce Long(1) to String

I don't know what node has a value with a datatype Long(1).

Is there a way around these errors?

Oh, never mind, I found it:

MATCH (n)
RETURN apoc.coll.toSet(apoc.coll.flatten(collect(keys(n)))) AS keys