Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
03-11-2021 06:16 AM
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
03-11-2021 06:51 AM
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), ", ")
03-13-2021 01:13 AM
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?
03-15-2021 03:54 AM
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), ", ")
03-20-2021 03:19 AM
Works like a charm, thank you 🙂
All the sessions of the conference are now available online