Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
08-03-2020 09:38 AM
Just discovered Graph and Neo4j a couple of days ago and I must say I really like what it has to offer. I'm still tinkering around still, and now I wonder how I would proceed with conditions inside queries.
In the below code, how would I delete the relationship in the same query if the quantity is set to 0?
I have a hard time understanding the syntax of the conditionals inside Cypher.
result = session.run(
f'MATCH (order:Order {{ ref: "SomeOrder" }})<-[rel:IN_ORDER]-(product:Product {{ name: "SomeProduct" }}) '
f'SET rel += {{ quantity: 0 }} '
f'WITH order '
f'MATCH (order)<-[r:IN_ORDER]-(p:Product) '
f'RETURN order, p.name, p.price * r.quantity AS total_unit_price '
)
08-03-2020 01:02 PM
Hello! Welcome to the community. If that only thing your checking is the quantity on the relationship you can just do:
match(o:Order)<-[r:IN_ORDER]-(p:Product)
where r.quantity = 0
delete r
return o, p
That will check to see that the quantity is zero and if so delete the relationship.
08-04-2020 12:21 AM
Hello @andre.krosby1992 and welcome
If there are a lot of relations to delete, the best way to delete is:
CALL apoc.periodic.iterate('MATCH (o:Order)<-[r:IN_ORDER]-(p:Product)
WHERE r.quantity = 0 RETURN r', 'DELETE r', {batchSize:1000})
Regards,
Cobra
08-04-2020 11:02 AM
Also you can reference this article if you're looking for more info on conditional Cypher execution:
All the sessions of the conference are now available online