# Combine the result of match and use for further processing

**URL:** <https://community.neo4j.com/t/combine-the-result-of-match-and-use-for-further-processing/11552>\
**Category:** Cypher\
**Tags:** cypher\
**Created:** [November 3, 2019, 1:27am UTC](https://community.neo4j.com/t/combine-the-result-of-match-and-use-for-further-processing/11552 "2019-11-03T01:27:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dowanna](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dowanna/32/6745_2.png) [@Dowanna](https://community.neo4j.com/u/Dowanna)\
**Post date:** [November 3, 2019, 1:27am UTC](https://community.neo4j.com/t/combine-the-result-of-match-and-use-for-further-processing/11552/1 "2019-11-03T01:27:28Z")

</div>

I'm developing an SNS with neo4j, and would like to know how to achieve the case described below.  
Please note that the use-case may not sound practical. It's because I'm trying to simplify the case. The actual conditions are much complicated, so I didn't want to write everything here.

Let's say I want to fetch all comments and its author by two conditions.

1. I want a comment by andy
2. I want a comment by my friends

To MATCH with multiple conditions, I have the following:

```auto
match (u1:User{name:'andy'})-[:CREATED]->(c1:Comment)
match (:User{name:'me'})-[:FRIENDS_WITH]->(u2:User)-[:CREATED]->(c2:Comment)
return u1,u2,c1,c2

```

However, to further process the User and Comment nodes, I want to pass these nodes with WITH, whilst combining them. Something like this would be ideal:

```auto
match (u1:User{name:'andy'})-[:CREATED]->(c1:Comment)
match (:User{name:'me'})-[:FRIENDS_WITH]->(u2:User)-[:CREATED]->(c2:Comment)
WITH (u1,u2) as u, (c1,c2) as c
...

```

Since that's not possible, I made several different attempts:

```auto
match (u1:User{name:'andy'})-[:CREATED]->(c1:Comment)
match (:User{name:'me'})-[:FRIENDS_WITH]->(u2:User)-[:CREATED]->(c2:Comment)
WITH collect(u1) + collect(u2) as u, collect(c1) + collect(c2) as c
UNWIND u
UNWIND c
...

```

But this would destroy the relation who created the comment, so doesn't work.

```auto
match p1=(u1:User{name:'andy'})-[:CREATED]->(c1:Comment)
match p2=(:User{name:'me'})-[:FRIENDS_WITH]->(u2:User)-[:CREATED]->(c2:Comment)
WITH p1+p2 as p
...

```

This doesn't work because + requires a list as argument.

UNION does not work because it must be followed by RETURN, but there's a ton of post-processing to do after this query.

How can I achieve such query?

---

<div class="post-metadata">

**Author:** ![mark.needham](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/mark.needham/32/104_2.png) [@mark.needham](https://community.neo4j.com/u/mark.needham)\
**Post date:** [November 4, 2019, 1:16pm UTC](https://community.neo4j.com/t/combine-the-result-of-match-and-use-for-further-processing/11552/2 "2019-11-04T13:16:30Z")

</div>

You could try collecting on a map that contains the user and the relationship/comment perhaps. So something like this:

```auto
match (u1:User{name:'andy'})-[created:CREATED]->(c1:Comment)
match (:User{name:'me'})-[:FRIENDS_WITH]->(u2:User)-[created2:CREATED]->(c2:Comment)
WITH collect(u1) + collect(u2) as u, collect(c1) + collect(c2) as c

WITH collect({user: u1, relType: created, comment: c1}) +
          collect({user: u2, relType: created2, comment: c2})

```

Not sure if that's what you want?

---

<div class="post-metadata">

**Author:** ![andrew\_bowman](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/andrew_bowman/32/73_2.png) [@andrew\_bowman](https://community.neo4j.com/u/andrew_bowman)\
**Post date:** [November 4, 2019, 7:43pm UTC](https://community.neo4j.com/t/combine-the-result-of-match-and-use-for-further-processing/11552/3 "2019-11-04T19:43:24Z")

</div>

Hello, here's an article on different ways to do [post-UNION processing](https://neo4j.com/developer/kb/post-union-processing/), that should have some workarounds you can use.

And just to note there are some features coming in 4.0 that will make this much easier to tackle.

---

<div class="post-metadata">

**Author:** ![Dowanna](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dowanna/32/6745_2.png) [@Dowanna](https://community.neo4j.com/u/Dowanna)\
**Post date:** [November 11, 2019, 8:58am UTC](https://community.neo4j.com/t/combine-the-result-of-match-and-use-for-further-processing/11552/4 "2019-11-11T08:58:01Z")

</div>

Thank you mark!

I'm not sure how that query works. In the second WITH you are using u1, but the first WITH only passes u and c, so it doesn't work on my local environment...! ☹

Also wouldn't two consecutive MATCH mean BOTH conditions must be met?  
I apologize my description wasn't detailed enough, but I need EITHER conditions met! 🙂
