# Map function OR returns boolean instead of properties

**URL:** <https://community.neo4j.com/t/map-function-or-returns-boolean-instead-of-properties/25877>\
**Category:** Newbie Questions\
**Created:** [September 24, 2020, 6:53am UTC](https://community.neo4j.com/t/map-function-or-returns-boolean-instead-of-properties/25877 "2020-09-24T06:53:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tarendran.vivekanand](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/tarendran.vivekanand/32/3909_2.png) [@tarendran.vivekanand](https://community.neo4j.com/u/tarendran.vivekanand)\
**Post date:** [September 24, 2020, 6:53am UTC](https://community.neo4j.com/t/map-function-or-returns-boolean-instead-of-properties/25877/1 "2020-09-24T06:53:34Z")

</div>

Hi all,

I am trying to map the results of my match when there are 2 different searches into 1.  
I added my code below

```auto
UNWIND $data AS row
OPTIONAL MATCH (a:NodeA)
WHERE a.id = row.id
OPTIONAL MATCH (a)-[:REL_2]->(c1:NodeC)
WITH a, collect(c1) AS lista
OPTIONAL MATCH (a)-[:REL_1]-(:NodeB)-[:REL_2]->(c2:NodeC)
WITH a, lista, collect(c2) AS listb
RETURN a{.*, List: lista OR listb} AS search

```

However this returns a boolean which is not the return I want. What I want is the properties of nodeC for each NodeA, since NodeA can only either have one the relationships and not both. Thus the result for example would be like:

`{a_properties: 'value', List: [{ key: value }] }`  
Whereby if lista is empty display listb or if listb is empty display lista

Thanks in advance

---

<div class="post-metadata">

**Author:** ![sunny\_pelletier](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/sunny_pelletier/32/6547_2.png) [@sunny\_pelletier](https://community.neo4j.com/u/sunny_pelletier)\
**Post date:** [September 24, 2020, 11:45am UTC](https://community.neo4j.com/t/map-function-or-returns-boolean-instead-of-properties/25877/2 "2020-09-24T11:45:28Z")

</div>

I think a `CASE WHEN x THEN y ELSE z END` could solve your problem. Can you try this:

```auto
WITH a, (CASE WHEN size(lista) == 0 THEN listb ELSE lista END) as list
RETURN a{.*, List: list} 

```

---

<div class="post-metadata">

**Author:** ![mdfrenchman](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/mdfrenchman/32/6094_2.png) [@mdfrenchman](https://community.neo4j.com/u/mdfrenchman)\
**Post date:** [September 24, 2020, 12:11pm UTC](https://community.neo4j.com/t/map-function-or-returns-boolean-instead-of-properties/25877/3 "2020-09-24T12:11:34Z")

</div>

@tarendran.vivekanand, OR is a boolean operator in cypher will always give you a boolean.

`CASE` statement as @sunny_pelletier outlined would be a good way to solve that.

---

<div class="post-metadata">

**Author:** ![tarendran.vivekanand](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/tarendran.vivekanand/32/3909_2.png) [@tarendran.vivekanand](https://community.neo4j.com/u/tarendran.vivekanand)\
**Post date:** [September 25, 2020, 1:55am UTC](https://community.neo4j.com/t/map-function-or-returns-boolean-instead-of-properties/25877/4 "2020-09-25T01:55:36Z")

</div>

Thank you, this worked for me

---

<div class="post-metadata">

**Author:** ![tarendran.vivekanand](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/tarendran.vivekanand/32/3909_2.png) [@tarendran.vivekanand](https://community.neo4j.com/u/tarendran.vivekanand)\
**Post date:** [September 25, 2020, 1:56am UTC](https://community.neo4j.com/t/map-function-or-returns-boolean-instead-of-properties/25877/5 "2020-09-25T01:56:08Z")

</div>

Thanks for the info @mdfrenchman, I did not know that

---

<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:** [October 26, 2020, 11:21pm UTC](https://community.neo4j.com/t/map-function-or-returns-boolean-instead-of-properties/25877/6 "2020-10-26T23:21:52Z")

</div>

We can actually simplify this by use of optional connections:

```auto
UNWIND $data AS row
OPTIONAL MATCH (a:NodeA)
WHERE a.id = row.id
OPTIONAL MATCH (a)-[:REL_1*0..1]->()-[:REL_2]->(c:NodeC)
WITH a, collect(c) AS List
RETURN a {.*, List} AS search

```

The `*0..1` means that the relationship may or may not exist, so the node it points to (the one that is empty in the pattern) may actually be the same node as `a`, having not traversed any relationship, or it may have traversed a relationship. From either of those, we traverse :REL\_2 to get to a :NodeC to fulfill the pattern, so we can use a single variable to address :NodeC nodes that are 1 or 2 hops away.

My approach here assumes that we don't need to check that a :NodeB exists between them, but if you DO need that restriction, then you can fix it with:

```auto
...
OPTIONAL MATCH path = (a)-[:REL_1*0..1]->()-[:REL_2]->(c:NodeC)
WHERE length(path) = 1 OR 'NodeB' IN labels(nodes(path)[1])
...

```

More about this technique here:

> **[Alternatives to UNION queries - Knowledge Base](https://neo4j.com/developer/kb/alternatives-to-union-queries/)**
>
> While UNIONs can be useful for certain cases, they can often be avoided completely with small changes to the query. In this article we’ll present various example cases where a…
