0

Using a db of users borrowing books from a library i want to create with 1 query 2 lendings that have the same date.

MATCH (u:User), (b:Book)
WHERE u.Name = 'Al' AND u.Surname = 'Pacino' AND (b.title = 'The Hours' OR 
b.title = 'War and Peace')
CREATE (u)-[:LEND {date:['16 March 2017']}]->(b)

I thought that would be doing what I wanted, but only 'The Hours' is created. What am I missing here?

Dave Bennett
  • 10,996
  • 3
  • 30
  • 41
Tasoulis Livas
  • 75
  • 1
  • 1
  • 9
  • what does `MATCH (u:User), (b:Book) WHERE u.Name = 'Al' AND u.Surname = 'Pacino' AND (b.title = 'The Hours' OR b.title = 'War and Peace') RETURN u.Name, b.title` return? – Dave Bennett Apr 26 '17 at 15:25
  • If you want to match 2 books, you should specify 2 books so that the whole query fails if one of the books is missing. Otherwise, like in your case, it will create only the relationships for books it was able to find. (This depends on the behavior you actually want) – Tezra Apr 26 '17 at 18:58
  • Possible duplicate of [Adding relationship to existing nodes with Cypher](http://stackoverflow.com/questions/20456002/adding-relationship-to-existing-nodes-with-cypher) – Otávio Barreto Apr 26 '17 at 20:32

2 Answers2

1

Why do you use only one 'b' for two books ? you can use as many as you want, for me you can do this:

MATCH (u:User), (b1:Book),(b2:Book)
WHERE u.Name = 'Al' AND u.Surname = 'Pacino' 
AND b1.title = 'The Hours' 
AND b2.title = 'War and Peace' 
CREATE (u)-[:LEND {date:['16 March 2017']}]->(b1) 
CREATE (u)-[:LEND {date:['16 March 2017']}]->(b2)
Norbert Feron
  • 46
  • 1
  • 5
0

You can create any number of relationships, all you need to do is pass in a list of properties to match on.

In this case, you could pass in a list of book titles:

MATCH (u:User)
WHERE u.Name = 'Al' AND u.Surname = 'Pacino' 
MATCH (b:Book)
WHERE b.title in ['The Hours', 'War and Peace'] // best to parameterize this
CREATE (u)-[:LEND {date:['16 March 2017']}]->(b)

If you have an index on :Book(title) then the :Book MATCH will use an index lookup.

InverseFalcon
  • 29,576
  • 4
  • 38
  • 51