BooleanExpression
public protocol BooleanExpression : ExpressionA BooleanExpression is an Expression that evaluates to a boolean value.
It is used to construct conditional logic within Firestore pipelines, such as in where
clauses or ConditionalExpression. BooleanExpression instances can be combined using standard
logical operators (&&, ||, !, ^) to create complex conditions.
Example usage in a where clause:
firestore.pipeline()
.collection("products")
.where(
Field("price").greaterThan(100) &&
(Field("category").equal("electronics") || Field("on_sale").equal(true))
)
-
Extension methodcountIf()
Creates an aggregation that counts the number of documents for which this boolean expression evaluates to
true.This is useful for counting documents that meet a specific condition without retrieving the documents themselves.
// Count how many books were published after 1980 let post1980Condition = Field("published").greaterThan(1980) firestore.pipeline() .collection("books") .aggregate([ post1980Condition.countIf().as("modernBooksCount") ])Declaration
Swift
func countIf() -> AggregateFunctionReturn Value
An
AggregateFunctionthat performs the conditional count. -
Extension methodthen(_:else:)
Creates a conditional expression that returns one of two specified expressions based on the result of this boolean expression.
This is equivalent to a ternary operator (
condition ? then : else).// Create a new field "status" based on the "rating" field. // If rating > 4.5, status is "top_rated", otherwise "regular". firestore.pipeline() .collection("products") .addFields([ Field("rating").greaterThan(4.5) .then(Constant("top_rated"), else: Constant("regular")) .as("status") ])Declaration
Swift
func then(_ thenExpression: Expression, else elseExpression: Expression) -> FunctionExpressionParameters
thenExpressionThe
Expressionto evaluate if this boolean expression istrue.elseExpressionThe
Expressionto evaluate if this boolean expression isfalse.Return Value
A new
FunctionExpressionrepresenting the conditional logic.