cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Create multiple relationships with partially exists nodes ids

Hi I need to perform the folowing query:

  1. Connect to one Node mulitple Nodes from diferent types and Ids. (get them as parameter)
  2. Part of the nodes with ids my not exists in the DB .
  3. Connect once ( not duplicate) only the nodes with with ids which are actually in the db.
  4. return the new updated code + a list of all not connected nodes (The non exists ids).

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

1 REPLY 1

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.