Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
10-19-2019 01:34 AM
Hi I need to perform the folowing query:
Example : We need to connect student Jack to 6 course with ids (1 to 6)
Jack is alleady connect to course id 2.
Course nodes with id 4 and 6 does not exits in the DB.
Jack will be connected to courses 1,2,3,5
Updated Student Node Jack + Courses 4 + 6 will be returned .
Thanks
10-24-2019 01:55 AM
Hi!
Welcome to the community. There are different Cypher syntax that'll be able to help you out on your way, for example:
1 - you will probably be using a COLLECT, a specified array of items or an UNWIND to help you out with matching against each item, e.g.
WITH [1,2,3,4,5,6] as courseIds
UNWIND courseIds as courseId
MATCH (c:Course {id:courseId})
2 - The above will only bring back nodes that exist with the ids, if they don't exist, they don't come back
3 - This is where the MERGE keyword will help out, once you've matched your student (s) and your course (c), you can use MERGE to connect them, and if there's already a connection there and all the properties (if any) match, it won't create another relationship, e.g.
MERGE (s)-[:STUDIES]->(c)
4 - You'll use the RETURN keyword here to bring back the elements you want. Re the courses that were not mapped/did not exist, you might use the collection expressions, e.g.
WITH [1,2,3] as c1, [2,3] as c2
RETURN filter(x IN c1 WHERE NOT x IN c2)
The Cypher reference card is very useful. Also, check out the Cypher documentation.
All the sessions of the conference are now available online