# Multiple node matching

**URL:** <https://community.neo4j.com/t/multiple-node-matching/47192>\
**Category:** Cypher\
**Tags:** apoc, cypher\
**Created:** [November 9, 2021, 2:50pm UTC](https://community.neo4j.com/t/multiple-node-matching/47192 "2021-11-09T14:50:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![armensanoyan](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/armensanoyan/32/17805_2.png) [@armensanoyan](https://community.neo4j.com/u/armensanoyan)\
**Post date:** [November 9, 2021, 2:50pm UTC](https://community.neo4j.com/t/multiple-node-matching/47192/1 "2021-11-09T14:50:36Z")

</div>

Hi. I am using Neo4j inside node JS and inside of node JS I have a list of nodes and relationships, which look like this (the structure of json can be changed if necessary)

```auto
[
  {name:'node name 1', id:uuid 1, ...}, // odd elements are nodes
  {type: 'WORKED_AT', from:'some date', until:'other date'}, // even elements are relationships
  {name:'node name 2', id:uuid 2, ...},
  {type: 'WORKED_AT', from:'some date', until:'other date'},
  ...
]

```

The length of array is variable/unknown. I need to do matching for this array

```auto
MATCH (nodeOne {name:$array[0].name})-[relOne:$arra[1].type {from:$arra[1].from}]-(nodeTwo {name:$array[2].name})-...

WHERE 
  nodeOne.ownerId = $ownerId and nodeTwo.ownerId = $ownerId ...

```

What is the best way to do this?

---

<div class="post-metadata">

**Author:** ![michael.hunger](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/michael.hunger/32/27377_2.png) [@michael.hunger](https://community.neo4j.com/u/michael.hunger)\
**Post date:** [November 14, 2021, 6:11pm UTC](https://community.neo4j.com/t/multiple-node-matching/47192/2 "2021-11-14T18:11:09Z")

</div>

Hmm it won't be that fast but you could try this:

```auto
WITH $array as data, length($array) as len
MATCH path=(start:Label)-[rel*..10]-(end:Label)
WHERE length(path)=len/2
AND all(idx IN range(0,len/2,2) where nodes(path)[idx/2].name = data[idx].name)
AND all(idx IN range(1,len/2,2) where type(relationships(path)[idx/2])= data[idx].type AND ...)

```

I think you would be better off (faster, cleaner) generating the structure of the query in javascript.

---

<div class="post-metadata">

**Author:** ![armensanoyan](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/armensanoyan/32/17805_2.png) [@armensanoyan](https://community.neo4j.com/u/armensanoyan)\
**Post date:** [November 16, 2021, 9:29am UTC](https://community.neo4j.com/t/multiple-node-matching/47192/3 "2021-11-16T09:29:23Z")

</div>

Hi Michael! Thank you for your replay. I already did it by JS and plus to what you said it gave me flexibility, for example to additionally do sum checking. I made the structure of query array

```auto
[
  {
    label: 'node Or Relationship Label Name',
    properties:[
    {
        type: 'string',
        key: 'property name',
        value: 'property value',
        operation: '='
      }
    ]
  }
]

```

So I got same structure of both nodes and relationships and by iterating over array I constructed separately `MATCH` part, where I checked the labels of nodes and relationships, `WHERE` part where I checked for node and relationship property matching and return part where I specified the nodes and relationships, which should be returned. At the end I just combined all parts and got the query.
