apoc.do.case

Details

Syntax

apoc.do.case(conditionals [, elseQuery, params ]) :: (value)

Description

For each pair of conditional queries in the given LIST<ANY>, this procedure will run the first query for which the conditional is evaluated to true. If none of the conditionals are true, the ELSE query will run instead.

Input arguments

Name

Type

Description

conditionals

LIST<ANY>

A list of conditionals, where each conditional is a pair: the first element is a predicate, and the second is a Cypher query to be executed based on that predicate.

elseQuery

STRING

A Cypher query to evaluate if all conditionals evaluate to false. The default is: ``.

params

MAP

A map of parameters to be used in the executed Cypher query. The default is: {}.

Return arguments

Name

Type

Description

value

MAP

The result returned from the evaluated Cypher query.

Usage Examples

The following will create a node with a name property of B, because that’s the first of the conditionals to evaluate to true:

CALL apoc.do.case([
  false,
  'CREATE (a:Node{name:"A"}) RETURN a AS node',
  true,
  'CREATE (b:Node{name:"B"}) RETURN b AS node'
  ],
  'CREATE (c:Node{name:"C"}) RETURN c AS node',{})
YIELD value
RETURN value.node AS node;
Results
node

(:Node {name: "B"})

The following will create a node with a name property of C, as per the elseQuery, because all conditionals evaluate to false:

CALL apoc.do.case([
  false,
  'CREATE (a:Node{name:"A"}) RETURN a AS node',
  false,
  'CREATE (b:Node{name:"B"}) RETURN b AS node'
  ],
  'CREATE (c:Node{name:"C"}) RETURN c AS node',{})
YIELD value
RETURN value.node AS node;
Results
node

(:Node {name: "C"})