apoc.do.when

Details

Syntax

apoc.do.when(condition, ifQuery, elseQuery [, params ]) :: (value)

Description

Runs the given read/write ifQuery if the conditional has evaluated to true, otherwise the elseQuery will run.

Input arguments

Name

Type

Description

condition

BOOLEAN

The predicate that determines whether to execute the ifQuery.

ifQuery

STRING

The Cypher statement to run if the condition is true.

elseQuery

STRING

The Cypher statement to run if the condition is false.

params

MAP

The parameters for the given Cypher statement. 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 A, as per the ifQuery, because the predicate is true:

CALL apoc.do.when(true,
  'CREATE (a:Node{name:"A"}) RETURN a AS node',
  'CREATE (b:Node{name:"B"}) RETURN b AS node',
  {}
)
YIELD value
RETURN value.node AS node;
Results
node

(:Node {name: "A"})

The following will create a node with a name property of B, as per the elseQuery, because the predicate is false:

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

(:Node {name: "B"})