unique - Node-Neo4j How to check if node exists(persistently)? -
i'm using node-neo4j client db. want create unique node, couldn't find how in documentation of node-neo4j. used logic below check if node exists or not:
person_param = {'name': namesurname, 'userid': userid }; person_node = db.createnode(person_param); if (!person_node.exists){ person_node.save(function(err, result){ //if(err) , if(!err) stuff }); }
but, understand, createnode
creates new node scratch, , when use exists
on it, checks if newly created node saved database or not.
how can check if node supplied properties exists in db or not?
thanks in advance.
the solution can think of, following:
- create properties object
- query neo4j instance properties
- if result returned use that
- otherwise save it
in code:
var person_param = {'name': namesurname, 'userid': userid }; // build cypher query var query = [ 'match (user: {name: {name}, id: {userid}})', 'return user' ].join('\n'); // use params in query var params = person_param; // send cypher query db.query(query, params, function (err, results) { if (err) throw err; // no node such properties found if(!results.length){ savenode(params, callback) } else { // carry on code... } }); function savenode(person_param, callback){ var person_node = db.createnode(person_param); person_node.save(function(err, result){ //if(err) , if(!err) stuff // call callback here }); }
the downside of approach need build cypher query each type of node have, because there's no way (as far know) pass both properties name , values cypher.
Comments
Post a Comment