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 :)