Remove relationships and then query

Hello,
I'm hoping there's a way to combine these two queries. My configuration is that I have :Person nodes and :Question nodes. A user is allowed to skip a question. In that case I add a :SKIPPED relationship between the :Person and :Question nodes.
When I query for :Question nodes to display, I exclude ones with a :SKIPPED relationship.
My goal is to allow the user to unskip the questions they already skipped. To do that, I'm using:

MATCH (p:Person {userId: 'test'})
MATCH (p)-[r:SKIPPED]-()
DELETE r

When they skip, I'd like to be able to return one of those questions with the same query. I tried using WITH to pass into another query, but it always returns 0 records even though running the query without the relationship delete, it returns 3.
The full query I tried is:

MATCH (p:Person {userId: 'test'})
MATCH (p)-[r:SKIPPED]-()
DELETE r
WITH p
MATCH(q: Question)-[:ISANANSWER]-(a: Answer) 
WHERE NOT(q)<-[*]-(p) 
AND NOT(q)-[:ISPENDING]->() 
RETURN q.questionId, q.value, collect(a) as answers LIMIT 1

Is this possible? Or do I need to just leave it as two separate queries?

Thanks in advance.

I'm not sure what you are attempting to do in the second code block, but you should be able to retrieve the question(s) for which the SKIPPED relationship was removed as follows:

MATCH (p:Person {userId: 'test'})-[r:SKIPPED]-(q:Question)
DELETE r
RETURN q

Elaine

1 Like

@elaine_rosenber beat me to it.

You don't even need the second query since you already know the questions you're skipping. To add something useful to the discussion:
If you want to unskip and return the question, one at a time:

MATCH (p:Person {userId: 'test'})-[r:SKIPPED]->(q:Question)
WITH p,r,q ORDER BY q.Qid LIMIT 1
DELETE r
RETURN q

The ORDER BY is only if you want to unskip in a specific order.

The second code block is where I was doing the removal of SKIPPED and then running a query. The additional aspect of a question not being included if it has a :PENDING relationship is also part of it. I'm going to add in the PENDING exclusion and try it. Will update shortly.

It's also collecting up the :Answer nodes and putting them in a list to be returned as well.

Thank you both! The statement executed beautifully. The final query I used was:

MATCH (p:Person {userId: $userId})-[r:SKIPPED]->(q:Question)<-[:ISANANSWER]-(a:Answer)
WHERE NOT (q)-[:ISPENDING]->()
DELETE r 
RETURN q.questionId, q.value, collect(a) as answers LIMIT 1

In case someone else is trying to do this, my end goal was to get an object that had the questionId and value from a :Question object and a list of answerId and value from the related :Answer nodes. I only returned non-pending and non-skipped questions. If the user runs out of questions, they can "unskip" the questions (purpose of this query). The query returns the following object:

{"questionId":"Wk9Q8AnNfO4B6U5","value":"This is Question 4","answers":[{"answerId":"z8W2i195KfbGgvx","value":"This is Answer 4-2"},{"answerId":"WfPbv1FUKXjA9LY","value":"This is Answer 4-3"},{"answerId":"JPvy4CMN2ZpGASx","value":"This is Answer 4-1"}]}

Thanks again for your help!

1 Like