Count of all related nodes

Hi everyone,

I am new to Neo4j and trying to find the count of all nodes grouped on label based on the following scenario where property of NodeE is not equal i.e. NodeE.prop <> NodeE.prop (E.g. end1 <> end2)

Expected count output Nodes badchild1 and subchild2 are ignored
NodeA -2
NodeB-2
NodeC-1
NodeD-1
NodeE-2

I would greatly appreciate any help.

  • neo4j version: 4.1.0
  • browser version: 4.0.11
  • apoc version: 4.1.04

Thanks

Hi neo4j.ardor,

My understanding of the problem is that in your result set you do not want to display nodes that have outgoing edges to same end nodes and those end nodes must have the label NodeE. So based on this understanding i propose the following solution:

  1. We extract nodes in the database that have node label NodeE and these nodes must have two incoming edges.
  2. We search for all the edges in the database and also information about the start node and end node of the edge.
  3. We only select those start nodes that have end nodes identified in 1.
  4. We collect the start nodes in a list in this case it would be badchild1 and subchild2. Additionally your result set also required me to add the node identified in 1 in to this list. So we add that node as well.
  5. We search for all the nodes in database and only find those nodes that do not equal to nodes identified in 4.
  6. We use the Labels(node) function and count function for aggregating the number of time nodes with certain label occur in the final result set.

QUERY
Match()-->(node2Delete:NodeE)<--()
With node2Delete as node2Delete
Match (startNode)-->(endNode)
Where endNode.name = node2Delete.name
With collect(distinct startNode.name) + collect(node2Delete.name) as nInclude
Match (allNodes)
Where not allNodes.name in nInclude
With Labels(allNodes) as labl
Return labl, count(labl)

DATABASE
Create (r1:NodeA{name:"root1"})-[:rel1]->(c1:NodeB{name:"child1"}),
(r2:NodeA{name:"root2"})-[:rel1]->(c2:NodeB{name:"child2"}),
(c1)-[:rel2]->(c2),
(c1)-[:rel3]->(sb1:NodeC{name:"subchild1"}),
(c1)-[:rel4]->(bch1:NodeD{name:"badchild1"}),
(c2)-[:rel3]->(sb2:NodeC{name:"subchild2"}),
(c2)-[:rel4]->(bch2:NodeD{name:"badchild2"}),
(sb1)-[:rel5]->(end1:NodeE{name:"end1"}),
(bch1)-[:rel5]->(end3:NodeE{name:"end3"}),
(sb2)-[:rel5]->(end3),
(bch2)-[:rel5]->(end2:NodeE{name:"end2"})

1 Like