red-devil
red-devil

Reputation: 1103

Neo4j, Cypher: Set avg rating of a director on the basis of his/her movies

Unable to write a query to set avg rating for a director.

Also, can't figure out how to use FOREACH in this case to avoid Invalid use of aggregating function , Type mismatch: expected Collection<T> but was Node errors.

Avg Rating of a director = Avg(Rating of all its movies)

Example query

MATCH (d:Director)
WITH d
MATCH (d)-[:Directed]->(m:Movie)
SET d.avg_rating = AVG(toFloat(COLLECT(m.rating)))

Please help out.

Upvotes: 0

Views: 138

Answers (2)

Satish Shinde
Satish Shinde

Reputation: 2996

You Can directly use

MATCH (d:Director)-[:Directed]->(m:Movie)
SET d.avg_rating = AVG(toFloat(m.rating))

Upvotes: 2

Luanne
Luanne

Reputation: 19373

The toFloat would apply directly to m.rating, and AVG doesn't need the COLLECT:

MATCH (d:Director)-[:Directed]->(m:Movie)
SET d.avg_rating = AVG(toFloat(m.rating))

(I also dropped the superfluous match d:Director, which has nothing to do with your error however)

Upvotes: 2

Related Questions