Gaurav Jain
Gaurav Jain

Reputation: 419

Creating Nodes and Relationships, after Matching Nodes using ID

I have 3 types of nodes - Challenge, Entry and User, and 2 relationships between these nodes: (Entry)-[POSTED_BY]->(User) and (Entry)-[PART_OF]->(Challenge).

Here is what I'm trying to accomplish: - I have an existing Challenge, and I can identify it by its ID (internal neo4j id) - I also have an existing User, and I can identify it by its id (not the same as neo4j internal id) - Given the existing Challenge and User, I need to create a new Entry node, and link it with the Challenge node with a PART_OF relationship.
- In addition, I need to link the new Entry with the User node by the POSTED_BY relationship - I want to achieve the above in a single Cypher query statement, if possible

This is what I'm trying:

MATCH (c:Challenge) 
WHERE (id(c) = 240), 
MATCH (u:User {id: '70cf6846-b38a-413c-bab8-7c707d4f46a8'}) 
CREATE (e:Entry {name: "My Entry"})-[:PART_OF]->(c), (u)<-[r:POSTED_BY]-(e)
RETURN e;

However, this is failing as it seems I cannot match two nodes with the above syntax. However, if I match the Challenge with a non-internal property, say name, it seems to work:

MATCH (c:Challenge {name: "Challenge Name"), 
MATCH (u:User {id: '70cf6846-b38a-413c-bab8-7c707d4f46a8'}) 
CREATE (e:Entry {name: "My Entry"})-[:PART_OF]->(c), (u)<-[r:POSTED_BY]-(e) 
RETURN e;

However, as I mentioned above, I want to match the neo4j internal Node ID for the Challenge, and I'm not sure if there's a way to match that other than by using the WHERE id(c) = 232 clause.

Upvotes: 0

Views: 71

Answers (1)

InverseFalcon
InverseFalcon

Reputation: 30397

Your syntax is almost correct, but you don't need the comma between the WHERE and the next MATCH.

MATCH (c:Challenge) 
WHERE id(c) = 240
MATCH (u:User {id: '70cf6846-b38a-413c-bab8-7c707d4f46a8'}) 
CREATE (e:Entry {name: "My Entry"})-[:PART_OF]->(c), (u)<-[r:POSTED_BY]-(e) 
RETURN e;

Upvotes: 1

Related Questions