0
votes

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.

1
Note that for production environments is not a good idea trust in neo4j internal IDs. From the docs: Neo4j reuses its internal ids when nodes and relationships are deleted. This means that applications using, and relying on internal Neo4j ids, are brittle or at risk of making mistakes. It is therefore recommended to rather use application-generated ids.Bruno Peres

1 Answers

1
votes

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;