FirebaseFirestore Framework Reference

BooleanExpression

public protocol BooleanExpression : Expression

A 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))
  )
  • countIf()

    Extension method

    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() -> AggregateFunction

    Return Value

    An AggregateFunction that performs the conditional count.

  • then(_:else:)

    Extension method

    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) -> FunctionExpression

    Parameters

    thenExpression

    The Expression to evaluate if this boolean expression is true.

    elseExpression

    The Expression to evaluate if this boolean expression is false.

    Return Value

    A new FunctionExpression representing the conditional logic.