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.