FQL v4 will be decommissioned on June 30, 2025. Ensure that you complete your migration from FQL v4 to FQL v10 by that date.

Fauna accounts created after August 21, 2024 must use FQL v10. These accounts will not be able to run FQL v4 queries or access the v4 Dashboard.

For more details, see the v4 EOL announcement and migration guide. Contact support@fauna.com with any questions.

If

This reference topic applies to FQL v4. Go to this page for the latest FQL v10 reference topics.

If( cond_expr, true_expr, false_expr )
if_( cond_expr, true_expr, false_expr )
If( cond_expr, true_expr, false_expr )
If( cond_expr, true_expr, false_expr )
If( cond_expr, true_expr, false_expr )
If( cond_expr, true_expr, false_expr )

Description

The If function evaluates and returns the true_expr or false_expr expression, depending on the value of the cond expression. If the cond expression evaluates to anything other than a Boolean, If returns an "invalid argument" error.

Any valid FQL expression is acceptable, including null.

The If function requires three parameters, and using them is equivalent to if-then-else. This means that you cannot use If to express if-then logic. Instead, you might find the Filter function useful, as it could be used to remove documents from consideration, in bulk, that might otherwise require If logic.

Parameters

Parameter Type Definition and Requirements

cond

Boolean Expression

The conditional expression to be evaluated and tested for true or false.

true_expr

Expression

The expression or variable to return if cond is true.

false_expr

Expression

The expression or variable to return if cond is false.

Returns

The evaluation of either the true_expr or false_expr expression, depending on the evaluation of the cond expression.

Examples

The following query evaluates the cond expression, which is the condition that needs to be tested, and then determines that cond returns true. This causes the second argument, the true_expr expression, to be evaluated and returned. The third argument, the false_expr expression, is never evaluated.

client.query(
  q.If(true, 'was true', 'was false')
)
.then((ret) => console.log(ret))
.catch((err) => console.error(
  'Error: [%s] %s: %s',
  err.name,
  err.message,
  err.errors()[0].description,
))
was true
result = client.query(
  q.if_(True, "was true", "was false")
)
print(result)
was true
result, err := client.Query(
	f.If(true, "was true", "was false"))

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
was true
try
{
    Value result = await client.Query(
        If(true, "was true", "was false")
    );
    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
StringV(was true)
System.out.println(
    client.query(
        If(Value(true), Value("was true"), Value("was false"))
    ).get());
"was true"
If(true, 'was true', 'was false')
'was true'
Query metrics:
  •    bytesIn:  48

  •   bytesOut:  23

  • computeOps:   1

  •    readOps:   0

  •   writeOps:   0

  •  readBytes:   0

  • writeBytes:   0

  •  queryTime: 3ms

  •    retries:   0

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!