将批量节点与属性合并时重复

Duplication when merging bulk nodes with attributes

我无法从文档中看到如何将批量数据与属性合并。这里的示例使用 R,但解决方案将是 Cypher。我需要将id指定为唯一字段,以便在更改属性或添加新属性数据时不会复制节点。

x = data.frame(id = 1:2, name = c('Kate','Charles'), 
               age = c(27,34), grp = c('A','B'), stringsAsFactors = F)

merge_df_with_graph = function(x, graph, query){
  t = newTransaction(graph)
  for (i in 1:nrow(x)) {
    appendCypher(t, query, id = x$id[i], name = x$name[i], 
                 age = x$age[i], grp = x$grp[i])
  }
  commit(t)
}

merge_df_with_graph(x, graph, "MERGE (n:User {id:{id}, name:{name}});")
n = getNodes(graph, 'MATCH (n) RETURN (n);')
length(n)
#> [1] 2

# when an attribute changes
x$name[2] = 'Charlie'
merge_df_with_graph(x, graph, "MERGE (n:User {id:{id}, name:{name}});")
n = getNodes(graph, 'MATCH (n) RETURN (n);')
length(n)
#> [1] 3

# when new attributes data is added
merge_df_with_graph(x, graph, "MERGE (n:User {id:{id}, name:{name}, age:{age}});")
n = getNodes(graph, 'MATCH (n) RETURN (n);')
length(n)
#> [1] 5

我是 neo4j 的新手,example in documentation 没有指定如何定义唯一键以外的属性。感谢您的帮助。

您只能在其他属性中使用 id 属性 至 MERGE 以及使用后的 SET

MERGE (n:User {id:{id})
SET n.name = 'Kate'

# (...)

MERGE (n:User {id:{id})
SET n.name = 'Charlie'
SET n.age = 22