Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node-Neo4j: How to check if cypherquery to delete node succeeded?

I am using node-neo4j npm module, and using the db.cypherquery() call to call cypher queries from my node js application. I am trying to delete a relationship between two nodes, and I would like to detect if the delete succeeded or failed.

Refer the code snippet below:

var cypherQuery = "MATCH (u1:User {id: '10'})-[r:LIKES]->(u2:User {id: '20'}) DELETE r;";
db.cypherQuery(cypherQuery, function(err, result){
            if(err) throw err; //does err indicate that delete failed, or something else (such as a syntax error in the cypher query)?

//do something based on whether delete succeeded or failed here

});

So, in the above, what is the best way to detect cases such as the below: 1) No matching relationship was found, so there is nothing to delete 2) Relationship was found and deleted successfully 3) Relationship was found but there was some other error in deleting it 4) There was a syntax error in the cypher script (I think this is detected via the err value)

like image 219
Gaurav Jain Avatar asked May 08 '17 14:05

Gaurav Jain


People also ask

How do I delete a node in Cypher?

To delete nodes and relationships using Cypher, use the DELETE clause. The DELETE clause is used within the MATCH statement to delete whatever data was matched. So, the DELETE clause is used in the same place we used the RETURN clause in our previous examples.

How delete all nodes and relationships in Neo4j give example?

There is a method to delete a node and all relationships related to that node. Use the DETACH DELETE statement: Example: MATCH (Kohli:player{name: "Virat Kohli"}) DETACH DELETE Kohli.

How do I delete a particular relationship in Neo4j?

In Neo4j to delete a node or relations between nodes you have to use DELETE clause. To delete any node you need DELETE clause with the MATCH statement, the MATCH statement data will find the specific node and whichever node is matched with the statement that node will be vanished.

Which of the given below syntax is used to remove a property from a node?

The REMOVE clause is used to remove properties from nodes and relationships, and to remove labels from nodes.


2 Answers

You can do DELETE and RETURN at the same time so DELETE r RETURN COUNT(r) will delete the matched r, and return the number of r deleted (or RETURN r for the list of r deleted)

like image 162
Tezra Avatar answered Oct 06 '22 01:10

Tezra


This is not well documented, but it is possible to access the number of nodes deleted like this:

result.summary.counters.nodesDeleted()

You'll see at my link that it references the StatementStatistics class, but that class isn't defined anywhere in the documentation (this seems like an omission, since it's a public API). You can find the definition of that class in the source of ResultSummary (and it has counters for relationships and a bunch of other stuff, too).

like image 33
rmeador Avatar answered Oct 06 '22 01:10

rmeador