You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无需APOC:用纯Cypher实现apoc.coll.intersection功能

纯Cypher实现数组交集的两种方法

如果你不想依赖APOC的apoc.coll.intersection函数,可以用以下两种纯Cypher方式实现相同的交集计算:

方法1:使用UNWIND+COLLECT(自动去重)

这种方式会自动对结果去重,和apoc.coll.intersection的行为完全一致:

// 保留原有查询上下文(假设你已匹配到childD节点)
WITH $detailedCriterionIds AS arr1, childD.detailedCriterionIds AS arr2
UNWIND arr1 AS item
WHERE item IN arr2
WITH DISTINCT item
COLLECT(item) AS intersection
// 后续可直接使用intersection变量,比如返回或用于其他逻辑
RETURN intersection

方法2:使用列表推导式(灵活控制去重)

列表推导式写法更简洁,若需要和APOC函数一样去重,可在数组前加上DISTINCT;若要保留原数组中的重复项,去掉DISTINCT即可:

// 保留原有查询上下文
WITH $detailedCriterionIds AS arr1, childD.detailedCriterionIds AS arr2
// 去重版(和APOC函数行为一致)
RETURN [x IN DISTINCT arr1 WHERE x IN arr2] AS intersection

补充说明

  • 两种方法都能精准筛选出两个数组的共同元素
  • 方法1天然完成去重,方法2可根据业务需求灵活调整是否保留重复元素

内容的提问来源于stack exchange,提问作者alexanoid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 00:44:59