apoc.do.case

Procedure APOC Core

apoc.do.case([condition, query, condition, query, …​], elseQuery:'', params:{}) yield value - given a list of conditional / writing query pairs, executes the query associated with the first conditional evaluating to true (or the else query if none are true) with the given parameters

Signature

apoc.do.case(conditionals :: LIST? OF ANY?, elseQuery =  :: STRING?, params = {} :: MAP?) :: (value :: MAP?)

Input parameters

Name Type Default

conditionals

LIST? OF ANY?

null

elseQuery

STRING?

params

MAP?

{}

Output parameters

Name Type

value

MAP?

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;
Table 1. 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;
Table 2. Results
node

(:Node {name: "C"})