2

I can't see from documentation how to merge bulk data with attributes. The example here uses R but the solution will be Cypher. I need to designate id as the unique field so that the node will not be duplicated when attributes are changed or new attribute data is added.

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

I'm new to neo4j and the example in documentation doesn't specify how to define attributes other than the unique key. Grateful for assistance.

geotheory
  • 22,624
  • 29
  • 119
  • 196

1 Answers1

2

You can use only the id property to MERGE and after use SET in other properties.

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

# (...)

MERGE (n:User {id:{id})
SET n.name = 'Charlie'
SET n.age = 22
Bruno Peres
  • 15,845
  • 5
  • 53
  • 89