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)
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.
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.
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.
The REMOVE clause is used to remove properties from nodes and relationships, and to remove labels from nodes.
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)
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With