Multiple node matching

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)

  {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

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

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

What is the best way to do this?

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

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.

1 Like

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

    label: 'node Or Relationship Label Name',
        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.