Cypher:类似于`sort -u`来合并2个集合?

ste*_*sin 7 neo4j cypher

假设我在一个属性中有一个带有集合的节点

START x = node(17) SET x.c = [ 4, 6, 2, 3, 7, 9, 11 ];
Run Code Online (Sandbox Code Playgroud)

和某个地方(即从.csv文件)我得到另一个价值集合,比方说

c1 = [ 11, 4, 5, 8, 1, 9 ]
Run Code Online (Sandbox Code Playgroud)

我把我的收藏品视为一套,元素的顺序无关紧要.我需要的是将xc与c1合并为魔术操作,以便生成的xc仅包含两者中的不同元素.想到了以下想法(尚未经过测试):

LOAD CSV FROM "file:///tmp/additives.csv" as row
START x=node(TOINT(row[0]))
MATCH c1 = [ elem IN SPLIT(row[1], ':') | TOINT(elem) ]
SET
x.c = [ newxc IN x.c + c1 WHERE (newx IN x.c AND newx IN c1) ];
Run Code Online (Sandbox Code Playgroud)

这不起作用,它将给出一个交集,但不是一个不同项的集合.更多RTFM给出了另一个想法:使用REDUCE()?但怎么样?

如何使用新的内置函数UNIQUE()扩展Cypher,它接受收集和返回集合,清理表单重复?

UPD.似乎FILTER()函数是接近但交叉点的东西:(

x.c = FILTER( newxc IN x.c + c1 WHERE (newx IN x.c AND newx IN c1) )
Run Code Online (Sandbox Code Playgroud)

WBR,Andrii

Dav*_*ett 4

像这样的事情怎么样...

with [1,2,3] as a1
, [3,4,5] as a2
with a1 + a2 as all
unwind all as a
return collect(distinct a) as unique
Run Code Online (Sandbox Code Playgroud)

添加两个集合并返回不同元素的集合。

2014 年 12 月 15 日 - 这是我的答案的更新......

我从 neo4j 数据库中的一个节点开始......

//create a node in the DB with a collection of values on it
create (n:Node {name:"Node 01",values:[4,6,2,3,7,9,11]}) 
return n
Run Code Online (Sandbox Code Playgroud)

我创建了一个包含两列的 csv 示例文件...

Name,Coll
"Node 01","11,4,5,8,1,9"
Run Code Online (Sandbox Code Playgroud)

我创建了一个 LOAD CSV 语句...

LOAD CSV 
WITH HEADERS FROM "file:///c:/Users/db/projects/coll-merge/load_csv_file.csv" as row

// find the matching node 
MATCH (x:Node) 
WHERE x.name = row.Name

// merge the collections
WITH x.values + split(row.Coll,',') AS combo, x

// process the individual values
UNWIND combo AS value

// use toInt as the values from the csv come in as string
// may be a better way around this but i am a little short on time
WITH toInt(value) AS value, x

// might as well sort 'em so they are all purdy
ORDER BY value
WITH collect(distinct value) AS values, x
SET x.values = values
Run Code Online (Sandbox Code Playgroud)

  • gmm 谢谢您指向正确方向的指针,我应该[更好地阅读文档:那里有“创建一个不同的集合”一章](http://neo4j.com/docs/stable/query-unwind.html#unwind -创建一个不同的集合):) (2认同)