Rodolfo Helfenstein
Rodolfo Helfenstein

Reputation: 5

Going through a list in Neo4j

I'm having trouble with lists in neo4j. I need to go through a list and compare each element with a property of another node. I tried something like this

MATCH (C:Courses),(S:Student) 
WHERE
  C.courseid = "8" AND
  S.userid = FOREACH(l IN SPLIT(C.students,",")
    CREATE (C)<-[E:enrolled]-(S) 
RETURN E

where C.students is a string of values separated with ",". FOREACH was the only function that I found to do this, but I don't think I can use it that way. Any ideas of how I can do that?

Upvotes: 0

Views: 44

Answers (1)

cybersam
cybersam

Reputation: 67019

This query should do what you intended:

MATCH (c:Courses)
WHERE c.courseid = '8'
MATCH (s:Student)
WHERE s.userid IN SPLIT(c.students, ',')
CREATE (c)<-[e:enrolled]-(s)
RETURN e

If you have a significant number of courses or students, then you should also create indexes for :Courses(courseid) and/or :Student(userid).

Upvotes: 1

Related Questions