Aerodynamika
Aerodynamika

Reputation: 8423

How to transform an UNWIND query to FOREACH in Neo4J Cypher?

I have the following Neo4J Cypher query:

UNWIND $mentionsRelations as mention 
MATCH (u:User{name:mention.from}) 
RETURN u.uid;

The params are:

{
  "mentionsRelations": [
    {
      "from": "a",
      "to": "b"
    },
    {
      "from": "c",
      "to": "d"
    }
  ]
}

Is it possible to rewrite it to get the same results using the FOREACH query?

I know it doesn't accept the MATCH parameter, but I'm just curious if there's a workaround to get the same results?

Basically what I want is to reiterate through the mentionsRelations using FOREACH and to then output any matches in the database it uncovers.

Thanks!

Upvotes: 0

Views: 471

Answers (1)

InverseFalcon
InverseFalcon

Reputation: 30417

Not currently possible, FOREACH is only for write operations and can't be used for just matching to nodes.

Is there some reason UNWIND won't work for you?

You can also do a match based upon list membership, which should work similarly, though you'd have to extract out the values to use for the property match:

WITH [mention in $mentionsRelationships | mention.from] as froms
MATCH (u:User) 
WHERE u.name in froms
RETURN u.uid;

Upvotes: 1

Related Questions