问题
I have 2 hierarchies in my database.
Hierarchy 1:
Company{name:'ABC', CompanyId:1,} <-- Category <-- SubCategory <-- Service <-- Asset <-- Anomaly
Hierarchy 2:
Company{name:'XYZ', CompanyId:21,} <-- Category <-- Service <-- Asset <-- Anomaly
What is the best way to query node labels in my hierarchy for a CompanyId? I am looking for an output in the following format:
CompanyId:1
Company Category SubCategory Service Anomaly
CompanyId:2
Company Category Service Anomaly
Thanks.
回答1:
Assuming that every company hierarchy chain begins with a company node that has the label Company
and always and with an node with the Anomaly
label, you could go after paths of the company hierarchy and return the list of labels for each chain.
match p=(:Company)<-[*]-(:Anomaly)
with head(nodes(p)) as Company, p
return Company.CompanyId, reduce(labels = "", n IN nodes(p) | labels + labels(n)[0] + " ")
来源:https://stackoverflow.com/questions/28681685/neo4j-listing-node-labels