Reputation: 2670
I'm using Node-Neo4j client for my db. I want to create unique node, but I couldn't find how to do that in the documentation of Node-Neo4j. I used the logic below to check if a 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) and if(!err) stuff
});
}
But, from what I understand, createNode
creates a new node from scratch, and when I use exists
on it, it just checks if the newly created node saved into database or not.
How can I check if a node with supplied properties already exists in db or not?
Thanks in advance.
Upvotes: 0
Views: 1334
Reputation: 9989
The only solution I can think of, is the following:
In code:
var person_param = {'name': namesurname, 'userid': userid };
// Build a Cypher query
var query = [
'MATCH (user: {name: {name}, id: {userid}})',
'RETURN user'
].join('\n');
// use your params in the query
var params = person_param;
// Send the Cypher query
db.query(query, params, function (err, results) {
if (err) throw err;
// no node with such properties found
if(!results.length){
saveNode(params, callback)
} else {
// carry on with your code...
}
});
function saveNode(person_param, callback){
var person_node = db.createNode(person_param);
person_node.save(function(err, result){
//if(err) and if(!err) stuff
// call the callback here
});
}
The downside of this approach is that you need to build a Cypher query for each type of node you have, because there's no way (as far as I know) to pass both properties name and values to Cypher.
Upvotes: 1