I am adding iterative data to my neo4j database, but I am stuck on how to overwrite or update existing data and check if there is data there.
Basically, I have a set of movies with their respective identifiers, for example:
[ {id: 'gameofthrones', genre: 'fantasy', release: '2017'}, {id: 'inception', genre: 'scifi', release: '2010'}, ... ]
I can add movies as follows:
CREATE (m1:Movie {id: 'gameofthrones', genre: 'fantasy', release: '2017'}), (m2:Movie {id: 'inception', genre: 'scifi', release: '2010'})
However, when I run the script twice, then it creates 4 nodes instead of storing on two nodes.
So my question is, how can I make sure that it checks if the node id
present, and if so, overwrite it instead of creating a new node?
I tried (but only properties are added)
// data attributes['id'] = 'gameofthrones'; attributes['genre'] = 'fantasy'; ... // query MERGE ( s:Movie {attributes}.id) ON CREATE SET ( s:Movie {attributes} )
which I NodeJS
in NodeJS
as follows:
executeQuery(queryStr, {"attributes": attributes}) // cypher (nodejs) function executeQuery(queryStr, params) { var qq = Q.defer(); db.cypher({ query: queryStr, params: params, }, function (error, results) { if (error) { qq.reject(error); } else { if (results.length != 0) { qq.resolve(true); } else { qq.resolve(false); } }; }); return qq.promise; };