neo4j过滤器来自collect

时间:2015-10-22 15:42:23

标签: filter neo4j cypher collect

我的问题是这个,我有一个像这样的查询:

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

我无法找到过滤'产品'结果的方法,例如,数组'products'中包含productName ='pname1'的行:

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

上面的例子只会返回row1

提前感谢您的关注

1 个答案:

答案 0 :(得分:1)

WITH 子句是一个非常有趣的事情,可以将查询拆分,此时的查询结果可以保存到变量中。

因此,要仅获取第一行,查询将为

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}

修改

上面的解决方案比较整个地图,如果有更多属性,这有点难看。仅比较productName查询有点棘手:

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}