How to add unique data to the neo4j chart database

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; }; 
+6
source share
2 answers

you must change your request to this

 MERGE ( s:Movie {attributes}.id) ON CREATE SET s += {attributes} ON MATCH SET s += {attributes} // optional 

this should work, but you should use apoc.map.clean (), so you are not setting the identifier twice, which may cause some problems.

 MERGE ( s:Movie {attributes}.id) ON CREATE SET s += apoc.map.clean({attributes},['id'],[]) 
+4
source

You can achieve this with the MERGE as follows

 MERGE (m1:Movie {id: 'gameofthrones'}) ON CREATE SET m1.genre = 'fantasy', m1.release = '2017' MERGE (m2:Movie {id: 'inception'}) ON CREATE SET m2.genre: 'scifi', m2.release = '2010' 

Ideally, you want to create queries with parameters instead of literal strings. You can achieve this if user apoc.load.json

 with "file:///home/sample.json" as url // can be also http://url/sample.json CALL apoc.load.json(url) yield value UNWIND value as item MERGE (m1:Movie {id: item.id}) ON CREATE SET m1.genre = item.genre, m1.release = item.release 

dynamic property example with apoc functions:

 with "file:///home/sample.json" as url // can be also http://url/sample.json CALL apoc.load.json(url) yield value UNWIND value as item MERGE (m1:Movie {id: item.id}) ON CREATE SET m1 += apoc.map.clean(item,['id'],[]) 

or if you don't have apoc plugin:

 with "file:///home/sample.json" as url // can be also http://url/sample.json CALL apoc.load.json(url) yield value UNWIND value as item MERGE (m1:Movie {id: item.id}) ON CREATE SET m1 += item 

note that the identifier will be merged first and then updated with ON CREATE SET , and you want to avoid writing one property twice, using apoc and the above request, we can achieve this

+3
source

Source: https://habr.com/ru/post/1015709/


All Articles