MonkeyBonkey
MonkeyBonkey

Reputation: 47921

how to use two match statements in a cypher query

I'd like to combine two requests into one query and I'm not sure what happens when 2 match statements are used in a single cypher query.

say I have a list of friends and I'd like to see a list of my friends with each of their uncles and siblings listed in a collection. Can I have two match statements that would do the job? e.g.

match friends-[:childOf]->parents-[:brother]->uncles
    , friends-[:childOf]->parents<-[:childOf]-siblings
return friends, collect(siblings), collect(uncles)

However, if I do a query like this, it always returns no results.

Upvotes: 9

Views: 15990

Answers (2)

ean5533
ean5533

Reputation: 8994

You may want to make some of those relationships optional. For example, if you find a sibling but you don't find any uncles, this query will return null because it didn't satisfy both match clauses. If you make the end relationships optional then you don't have to satisfy both clauses completely to return data. So:

match friends-[:childOf]->parents-[?:brother]->uncles
    , friends-[:childOf]->parents<-[?:childOf]-siblings
return friends, collect(siblings), collect(uncles)

Upvotes: 1

Gopi
Gopi

Reputation: 10293

Since you have already chosen parents in your first match class, you can do like this -

match friends-[:childOf]->parents-[:brother]->uncles
with friends, parents, uncles
match parents<-[:childOf]-siblings
return friends, collect(siblings), collect(uncles)

Upvotes: 11

Related Questions