2

我无法从文档中看到如何将批量数据与属性合并。此处的示例使用 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 的新手,文档中的示例没有指定如何定义唯一键以外的属性。感谢您的帮助。

4

1 回答 1

2

您只能在其他属性中使用该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
于 2017-10-27T11:08:01.803 回答