Neo4j remove a node and return the remote node

Christmas of all

I have a simple question. I want to remove a node with / without relationships in Neo4j and return the remote node and / or its specific property. Something in the following lines (below does not work)

MATCH(j:JOB) where j.job_id= "1" DELETE j, return j;  

I can accomplish the above task in two different queries, request the node to be deleted, and then delete it, but I want to know if this can be done in one expression.

I wonder if there is a way to save the node in another placehoder and then remove the node and return the placeholder. I'm new to Neo4j, you need suggestions.

I came across this post , which is old, and I could not get it to work with my version of Neo4j. I am using Neo4j 2.3.1

+4
source share
2 answers

You can use the sentence WITHto alias the data (properties) you want to return and remove the node in the same query:

//WITH j, needed to add j after WITH for cypher to work.

MATCH(j:Job) where j.job_id = "1" 
WITH j, j.industry AS industry, j.name AS name
DELETE j
RETURN industry, name

See this answer .

+7
source

There may be an easier way to accomplish what you want.

Instead of copying the node, why not just leave it the same, change its label (so that it does not interfere with the rest of your model) and then return that node?

Something like that:

MATCH (j:JOB { job_id = '1' })
OPTIONAL MATCH (j)-[r]-(n)
REMOVE j:JOB
DELETE r
SET j:RecycleBin_JOB
RETURN j;

Copying a node to store this seems like a waste of time, as you already have it. Just return this and adjust the labels and relationships so that they do not interfere with the rest of your model.

+2
source

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


All Articles