tuna
tuna

Reputation: 6351

Calculate count of related nodes in neo4j

I have the following query which returns the name and id of categories but I also want the photo count related to that category. Is there a way to calculate the photo count and return with the bundle

MATCH (user:User {id:"xyz"),
       (root:Category {id:"123"})
MATCH path=(user)<-[:OWNER]-(photo)-[:PHOTO_OF]->(categories)
WITH distinct categories as cats, user, photo
MATCH catpath=(cats)<-[:CHILD_OF*]-(root)
WHERE photo.is_private=false
WITH [x in nodes(catpath) | x.id] as p, root, catpath, user
WHERE p[-1] = "123"
return distinct [x in nodes(catpath) | [x.id, x.name]] as paths

Upvotes: 0

Views: 232

Answers (1)

cybersam
cybersam

Reputation: 66967

Does this work for you?

MATCH
    (user:User {id:"xyz"}),
    (root:Category {id:"123"}),
    path=(user)<-[:OWNER]-(photo)-[:PHOTO_OF]->(categories)
WHERE photo.is_private=false
WITH DISTINCT categories as cats, root, COUNT(photo) AS count
MATCH catpath=(cats)<-[:CHILD_OF*]-(root)
RETURN {numPhotos: count, path: [x in nodes(catpath) | {id: x.id, name: x.name}]} AS result;

I have cleaned up the query as well, to make it more efficient and to remove redundant checks.

Upvotes: 1

Related Questions