2

MATCH (n:Topic { mongoId: {_id} })-[r]-() DELETE n,r RETURN r;

That returns the error 'Error: Relationship 1509 has been deleted'.

However, I need r.mongoId to delete entries in another database.

How do I do this with Neo4j 2.2.3?

I'm doing this through the Seraph library. Is there a way to collect a property, delete the relationships, and return the collection?

I just need this data: MATCH (n:Topic { mongoId: _id })-[r]-() RETURN COLLECT(r.mongoId);

Thanks!

Michael Cole
  • 15,473
  • 7
  • 79
  • 96

1 Answers1

5

You can use a WITH clause to alias the data you want to return before deleting the node and relationship. Something like this:

MATCH (n:Topic {mongoId: {_id} })-[r]-() 
WITH r.mongoId as docId, n,r 
DELETE n,r 
RETURN docId

Or you could break it up into two queries, one to retrieve the property you want, then the second to delete the relationship and node.

EDIT: you most likely want to specify a directed relationship to avoid duplicate docId properties returned:

MATCH (n:Topic {mongoId: {_id} })-[r]->()
...
William Lyon
  • 8,371
  • 1
  • 17
  • 22
  • I didn't fully understand your edit. Duplicate id's aren't so bad. But if I direct the relationship, I'll miss r in (n:)<-[r]-(), which would be an error. Did I misunderstand? – Michael Cole Jun 30 '15 at 13:59
  • 1
    You are right - the usefulness of a directed relationship pattern depends on your data model. If your data model allows for both (:Topic)-[]->() and (:Topic)<-[]-(), then yes you will want an undirected pattern. Just realize there may be duplicated relationships / relationship properties returned (which you can filter out with the `DISTINCT` command). – William Lyon Jun 30 '15 at 14:57