How do I set a node attribute to use a function when calculating its value?

StackOverflow https://stackoverflow.com/questions/23485565

  •  16-07-2023
  •  | 
  •  

سؤال

I'm trying to add to a user node an average rating attribute, and assign the value. I can get the userid, and the average successfully with the following cql query.

MATCH (n)-[r:RATES]->(m)
RETURN DISTINCT n.userid as userid, AVG(toFloat(r.rating)) as avgrating
ORDER BY n.userid

How do I go about adding the average to the n (user) node? I've tried a FOREACH with no success.

MATCH p = (n)-[r:RATES]->(m)
FOREACH (n IN nodes(p)| SET n.avgrating = AVG(toFloat(r.rating)))

And also a set

MATCH (n)-[r:RATES]->(m)
SET n.avgrating = AVG(toFloat(r.rating))
RETURN n.userid, n.avgrating)
ORDER BY n.userid;
هل كانت مفيدة؟

المحلول

MATCH (p:Person)-[r:RATES]->()
WITH p, AVG(r.rating) AS avg
SET p.avg_rating = avg
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top