0

My problem is this, I have a query like this one:

MATCH (a:A),
(a)-[:relation]-(b:B)
RETURN {name: a.name, products: COLLECT(DISTINCT {productName: b.name, 
ident: b.identifier}) }

and I can't find a way to filter the result of 'products', for example, having the rows with the productName = 'pname1' contained in the array 'products':

row1: {name: 'name', products:[{name: 'pname1', ident: 'id1'}, {name: 'pname3', ident: 'id3'}] }
row2: {name: 'name2', products:[{name: 'pname2', ident: 'id2'}] }

The example above would only return the row1

Thank you in advance for your attention

chris
  • 35
  • 5

1 Answers1

3

The WITH clause is a very interesting thing with which the query can be splitted and the query results at this point can be saved to variables.

So, to only get the first row, the query would be

MATCH (a:A),
(a)-[:relation]-(b:B)
WITH a, COLLECT(DISTINCT {productName: b.name, ident: b.identifier}) as products
WHERE {productName: "pname1", ident:"id1"} in products
RETURN {name: a.name, products: products}

Edit:

The solution above compares the whole map which is a bit ugly if there are more properties. To only compare the productName the query is a bit more tricky:

MATCH (a:A),
(a)-[:relation]-(b:B)
WITH a, COLLECT(DISTINCT {productName: b.name, ident: b.identifier}) as products
UNWIND products as prows
WITH a, prows, products
WHERE prows.productName = "pname1"
RETURN {name: a.name, products: products}
Brakebein
  • 2,197
  • 1
  • 16
  • 21
  • Thank you very much the second one did the trick, I don't know why the first wasn't giving me any results. Thanks again – chris Oct 23 '15 at 09:30