Gaurav Jain
Gaurav Jain

Reputation: 419

How to add multiple relationships to a single node in one query

Consider this example: I have one Author node, and several Book nodes. I want to create the WROTE relationship between the Author and several Book nodes in a single cypher Query statement. Also, the only way I have to look up the nodes for both the Author and all the Book nodes is by their node ID.

Here's what I tried:

MATCH (a:Author) WHERE id(a) = '31' 
MATCH (b0:Book) WHERE id(b0) = '32' 
MATCH (b1:Book) WHERE id(b1) = '33' 
CREATE  (b0)<-[:WROTE {order : '0'}]-(a) 
CREATE  (b1)<-[:WROTE {order : '1'}]-(a)  

However, it doesn't seem to work.

Thanks.

Upvotes: 2

Views: 1671

Answers (2)

Tomaž Bratanič
Tomaž Bratanič

Reputation: 6514

Neo4js native ids are stored as numbers, so do not match by string. Also Cypher has IN clause that lets you match arrays, so you can simplify you query to this

MATCH (a:Author) where id(a)=31
MATCH (b:Book) where id(b) in [32,33]
CREATE (a)-[:WROTE]->(b)

Upvotes: 3

Gaurav Jain
Gaurav Jain

Reputation: 419

Found the issue to be that I shouldn't be using the ' when matching node IDs!

Upvotes: 1

Related Questions