m415656
m415656

Reputation: 1

cypher query iteration for reusability and convienience

currently I have a large amount of small queries in .cypher files that a script runs and updates my graph. All these queries do similar things but have different conditions and I was wondering if there was some way to add a list/mapping of some kind so I can iterate through a single query in a .cypher to capture several cases in just one query instead of having several....I'll post an example cypher (that is wrong, doesnt work, and isnt my real db, but just so the idea I'm trying to achieve out there)

MAP myMap = ['Paris':'France', 'Rome':'Italy', 'London':'England'] 

FOREACH (mapping in myMap) {
  match (p:Place)
  where (p.city = mapping.1)
  and not (p)-[:hasCountry]->()

match (o:Country {name: mapping.2})

return

  p.id as location,
  o.id as newCountry
}

let me know if that makes sense and if there is some solution out there that would make it possible. It is just a series of .cypher files in IntelliJ and then script runs a bunch of them to make updates to a graph. (dummy data just so I can get the idea).

Upvotes: 0

Views: 40

Answers (1)

Charchit Kapoor
Charchit Kapoor

Reputation: 9284

You should use UNWIND:

UNWIND [{city: 'Paris', country:'France'}, 
{city:'Rome', country: 'Italy'}, 
{city:'London', country:'England'}] as mapping
match (p:Place)
where (p.city = mapping.city)
and not (p)-[:hasCountry]->()
match (o:Country {name: mapping.country})
return p.id as location, o.id as newCountry

Upvotes: 2

Related Questions